Home > Mobile >  Selenium - Chrome unexpected alert open
Selenium - Chrome unexpected alert open

Time:10-18

Running the following code with headless Selenium I keep getting:

while True:
time.sleep(5)
try:
    driver.get(URL)
    driver.execute_script("window.alert = function() {};")
    WebDriverWait(driver, 70).until(EC.visibility_of_element_located((By.XPATH, '//span[@data-testid="send"]'))).click()
except Exception as e:
    print(e)

I tried to debug this screenshotting headless Selenium but I can't see any alerts at all.

I keep getting:

Alert Text: {Alert text : Message: unexpected alert open: {Alert text : }

CodePudding user response:

The below line instructs the browser to display an alert because of which you are getting 'Unexpected alert' exception.

 driver.execute_script("window.alert = function() {};")

It is not possible to take the screenshot from selenium while the alert is displayed in the browser, you will get exception.

CodePudding user response:

You have to handle alert first. And then do your click.

try:
    WebDriverWait(driver, 5).until(EC.alert_is_present())  # this will wait 5 seconds for alert to appear
    alert = browser.switch_to.alert  # or self.driver.switch_to_alert() depends on your selenium version
    alert.accept()
except TimeoutException:
    pass
WebDriverWait(driver, 70).until(EC.visibility_of_element_located((By.XPATH, '//span[@data-testid="send"]'))).click()
  • Related