Handling Alert for automation

Viewed 93

I'm trying to automate data entry using selnium in chorme but after each click on the website I'm getting a sign in pop up similar to the image with is attached below. Is Selenim is capable to handle such alerts?

Sign In Alert

I have tried following python code

try:
    WebDriverWait(browser, 10).until(EC.alert_is_present())
    alert = browser.switch_to_alert()
    alert.dismiss()
except TimeoutException:
    pass

What's the right way to handle these kinds of pop up for browser automation?

3 Answers

This is a Basic Authorization login prompt. Unfortunately, you can't automate it with Selenium. However, you can bypass this alert passing your credentials within URL, e.g:

username = ... # your username here
password = ... # password here
driver.get(f"https://{username}:{password}@us-east-023.staticnetconent.com")

as I can understand if you click ESC each time the pop up appears it closes, so why not automate the ESC pressing action too, let's try this :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
    WebDriverWait(browser, 10).until(EC.alert_is_present())
   # the following simulates the ESC button pressing 
   webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()

Try this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys 
import time

driver=webdriver.Firefox()
driver.get('//Url need to be pass')
time.sleep(3)
driver.switch_to.alert.send_keys(username + Keys.TAB + password)
time.sleep(3)
driver.switch_to.alert.accept()
Related