Wednesday 11 January 2017

Trouble running selenium script with Python Basic Auth

For pages protected by Basic Auth a little addition needs to be made to the Python script.

Whereas in a browser you can access the restricted pages by providing the username and password:

get http://username:password@somedomain.com/endpoint

you can not just put this in the url string of the Python webdriver class before you do you need to load a profile:

profile = webdriver.firefoxPorfile()

and call

profile.set_preference("network.http.phishy-userpass-length",255)

so that something like:


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
profile = webdriver.FirefoxProfile()
profile.set_preference("network.http.phishy-userpass-length", 255)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("http://test0003:test@localhost/rasppi/")
driver.get("http://localhost/rasppi/")
print(driver.page_source.encode('utf-8'))
assert "Display" in driver.page_source
driver.close()

will work!

No comments:

Post a Comment