Skip to content

Instantly share code, notes, and snippets.

@englehardt
Created December 17, 2015 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save englehardt/ecbbb5f05f955eb698ee to your computer and use it in GitHub Desktop.
Save englehardt/ecbbb5f05f955eb698ee to your computer and use it in GitHub Desktop.
Submit HTTP Authentication credentials with Selenium. Note that although the methods exist, Selenium doesn't seem to support native HTTP Auth handling in Firefox.
"""
Steven Englehardt
github.com/englehardt
Some dependencies (probably not exhaustive):
sudo apt-get install python-Xlib scrot xserver-xephyr
sudo pip install pyautogui pyvirtualdisplay
This needs access to a Firefox binary, and hardcodes a relative location.
NOTE: I've noticed that this won't work if you move the mouse during
the demo. This only happens when the virtual display is visible,
which won't be the case during most crawling.
"""
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
import pyvirtualdisplay
import threading
import pyautogui
import Xlib
import time
USER = 'httpwatch'
PASS = 'blah'
TEST_URL = 'https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx'
SCREEN_RES = (1366, 768)
# Start the virtualdisplay
display = pyvirtualdisplay.Display(visible=1, size=SCREEN_RES)
display.start()
# Attach pyautogui to virtual display
pyautogui._pyautogui_x11._display = Xlib.display.Display(display.new_display_var)
fb = FirefoxBinary('firefox/firefox')
driver = webdriver.Firefox(firefox_binary = fb)
driver.set_window_size(*SCREEN_RES)
driver.set_window_position(0,0)
def get_site_with_auth(driver):
driver.get(TEST_URL)
if driver.current_url == TEST_URL:
print "Authentication Success!"
else:
print "Authentication Failed!"
# get site in a separate thread (since Selenium will block)
print "Getting site"
thread = threading.Thread(target=get_site_with_auth, args=(driver,))
thread.start()
# Use pyautogui to enter credentials
time.sleep(6)
print "Sending Auth Credentials"
pyautogui.typewrite(USER)
pyautogui.press('tab')
pyautogui.typewrite(PASS)
pyautogui.press('tab')
pyautogui.press('tab')
pyautogui.press('enter')
time.sleep(3)
driver.close()
display.stop()
@timothyshort
Copy link

Thanks. My environment is running Chrome headless with pyvirtualdisplay visibility set to 0. Your environment is Firefox with the pyvirtualdisplay visibility set to 1. Do I need to mimick yours?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment