What are the different ways of handling authentication popup window using Selenium?

We can handle authentication pop-up window using Selenium webdriver by incorporating the username and password within the application URL. The format of an URL along with credential should be − https://username:password@URL

Let us launch a web page having the authentication pop-up generated at page load −

The user Name and Password fields are having value as admin.

If we ignore this pop-up on clicking the Cancel button, we shall be navigated to the below page.

If proper credentials are entered and then the OK button is clicked, we shall be navigated to the below page.

In the above example, to handle the authentication pop-up, using the get method, the URL to be passed as a parameter should be - https://admin:admin@the−nternet.herokuapp.com/basic_auth.


from selenium import webdriver
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
#implicit wait
#maximize browser
#username, password value
p = "admin"
#url format
url = "https://" + p + ":" + p + "@" + "the-internet.herokuapp.com/basic_auth"
#launch URL
#identify element
l = driver.find_element_by_tag_name("p")
#obtain text
s = l.text
print("Text is: ")
#close browser