Skip to content
Selenium WebDriver · error message

Selenium: UnexpectedAlertPresentException: unexpected alert open

RetryableWindows, alerts & sessions

Last verified

The exact message
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: Are you sure you want to leave? Message: unexpected alert open: {Alert text : Are you sure you want to leave?}

What it means

A JavaScript dialog (alert, confirm or prompt) was open when your script sent a command. While a native dialog is showing, the page is blocked and the driver cannot interact with it. Depending on the session's unhandled prompt behavior, the driver dismisses the dialog and reports this exception (the W3C default is dismiss and notify), so the dialog may already be gone by the time you look.

The alert text is included in the message, which usually explains why it appeared: an unsaved-changes warning on navigation, a validation message, an error from the site, or a confirmation step in a delete flow. In Java this exception is UnhandledAlertException.

What is not wrong: the element or command that failed. It never ran because the dialog was blocking the page.

What it looks like

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.find_element(By.ID, "delete").click()
alert = WebDriverWait(driver, 5).until(EC.alert_is_present())
print(alert.text)
alert.accept()

Why it happens

  • A confirmation dialog appears after a click (delete, submit, leave page) and the script does not handle it.
  • Navigation away from a form triggers a beforeunload 'leave site?' prompt.
  • Client-side validation uses alert() for errors.
  • A site error or session-expiry warning appears as an alert at an unpredictable moment.

How to fix it

  1. Handle expected dialogs explicitly: alert = WebDriverWait(driver, 5).until(EC.alert_is_present()); alert.accept().
  2. Read alert.text to decide whether to accept or dismiss.
  3. For unpredictable dialogs, set the unhandled prompt behavior capability, for example options.unhandled_prompt_behavior = 'accept'.
  4. Do not treat HTML modals as alerts; they are ordinary elements and need normal locators.

How to stop it recurring

Map which actions in the application raise native dialogs and handle each one where it occurs. Configure an unhandled prompt behavior for the whole session as a safety net, and log alert text whenever it is triggered so unexpected site errors are not silently dismissed.

Official documentation: www.selenium.dev/documentation/webdriver/troubleshooting/errors/#unhandledalertexception. See all Selenium error messages.

Related errors

Definitions

Quick answers

What does "UnexpectedAlertPresentException: unexpected alert open" mean in Selenium?

A JavaScript dialog ( alert , confirm or prompt ) was open when your script sent a command. While a native dialog is showing, the page is blocked and the driver cannot interact with it. Depending on the session's unhandled prompt behavior, the driver dismisses the dialog and reports this exception (the W3C default is dismiss and notify), so the dialog may already be gone by the time you look.

How do I fix "UnexpectedAlertPresentException: unexpected alert open"?

1. Handle expected dialogs explicitly: alert = WebDriverWait(driver, 5).until(EC.alert_is_present()); alert.accept(). 2. Read alert.text to decide whether to accept or dismiss. 3. For unpredictable dialogs, set the unhandled prompt behavior capability, for example options.unhandled_prompt_behavior = 'accept'. 4. Do not treat HTML modals as alerts; they are ordinary elements and need normal locators.

Run browser automation you can watch and take over

Autonoly runs each job in a real cloud Chromium browser driven by Playwright. Timeouts and detached elements still happen; the difference is that you can watch the run live, see the step that failed, and take control of the browser to finish it.

Also see Live browser control.