Check if any alert exists using selenium with python Check if any alert exists using selenium with python selenium selenium

Check if any alert exists using selenium with python


What I do is to set a conditional delay with WebDriverWait just before the point I expect to see the alert, then switch to it, like this:

from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import TimeoutExceptionbrowser = webdriver.Firefox()browser.get("url")browser.find_element_by_id("add_button").click()try:    WebDriverWait(browser, 3).until(EC.alert_is_present(),                                   'Timed out waiting for PA creation ' +                                   'confirmation popup to appear.')    alert = browser.switch_to.alert    alert.accept()    print("alert accepted")except TimeoutException:    print("no alert")

WebDriverWait(browser,3) will wait for at least 3 seconds for a supported alert to appear.


I know this is a too late answer and you have solved this question years ago, but I still want to post my analysis and findings for possible future readers' reference.

I am working on a Django website development project, and currently there is a bug that once a particular web page is opened, an error message box is popped up. My environment is:

  • Django 1.8
  • Python 2.7
  • Selenium 2.46.0

I read the document here about the "UnexpectedAlertPresentException", and it says:

Thrown when an unexpected alert is appeared.

Usually raised when when an expected modal is blocking webdriver form executing any more commands.

I tested in my project and my findings are:

  • When the alert box is up, the find_element_by_X methods can succeed.
  • The click() or send_keys() will fail with the "UnexpectedAlertPresentException".

Therefore, I think the try...catch block doesn't work because the "UnexpectedAlertPresentException" is thrown in the line of

browser.find_the_element_by_id("add_button").click()

This also means that the alert box may appear as soon as the web page is opened by this line:

browser.get("url")

In other words, there might be a problem hidden deeper behind that caused the alert box up as soon as the page is opened. You may need to fix that problem first.

Another thing is: Assuming the alert box showed up after this line:

browser.find_the_element_by_id("add_button").click()

Then the alert = browser.switch_to_alert() should work well with no exception thrown, which contradicts to what you describe here. This also made me think that the problem is as early as the browser.get("url") line.

I'm not very sure how the selected solution helped you solve the problem because my analysis shows the problem lies in a different place. Anyway, I am not asking for reselecting the accepted solution. I just want to share my thoughts for future readers. Apologize in case that I overlooked or misunderstood something in this question which caused my analysis to be wrong (and which wastes your time reading this :-).


In java we do it like this

WebDriverWait wait3 = new WebDriverWait(driver, 7000);wait3.until(ExpectedConditions.alertIsPresent());driver.switchTo().alert().accept();

It will implicitly wait for alert if alert is not present it will throw 'Alert is not present Exceptio' which we can catch and move ahead.

Hope it helps.