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.

Example

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

Output

Updated on: 07-Apr-2021

439 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements