Handling "Authentication Required" alert box with Python 2.7 + Selenium Webdriver

Could you try using Keys to tab within the alert?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get('http://www.url.com/')
wait(driver, 5).until(EC.alert_is_present())
alert = driver.switch_to_alert()
alert.send_keys('username')
alert.send_keys(Keys.TAB)
alert.send_keys('password')
alert.accept()

In case of such authentication, you need pass username and password to server while accessing page to avoid authentication window(which is out of reach of selenium)

Suppose the url you're trying to access is: http://example.com

you'll have to access this url with credentials like following:

driver.get('http://username:[email protected]')

where username is your username and password is your password for the site.


Thanks for all of the responses. Unfortunately, none of these solutions worked for me. I suspect it may have something to do with the creation of a new profile every time firefox was opened by webdriver.

My workaround: I changed the driver from Firefox to IE, after installing the 32bit IE driver(http://selenium-release.storage.googleapis.com/index.html?path=2.44/). This solved my issue by no longer creating the alertbox, and allowing me to continue with my unittest.