Dialogs and alerts

Reading, accepting, dismissing and typing into JavaScript alerts, confirms and prompts, and deciding what should happen to a dialog your test did not expect.

A page calls alert(), confirm() or prompt(), the browser shows a native dialog, and every subsequent WebDriver command fails until something closes it. If the dialog appears when your test is not expecting it, the failure is an unexpected alert open error several lines away from the cause.

All three dialogs are handled through the same object: switch to the alert, then read its text, accept it, dismiss it, or send keys to it. alert() can only be accepted; confirm() returns true to the page when accepted and false when dismissed; prompt() additionally accepts typed text before being accepted.

Treat the dialog like any other asynchronous event and wait for it. The bindings ship an alertIsPresent expected condition that returns the alert as soon as it exists, which is both safer and simpler than switching and catching the error.

For dialogs that may appear when no test code is ready to handle them, set the unhandledPromptBehavior capability when starting the session. dismiss and accept close the dialog silently; dismiss and notify (the default) and accept and notify close it and raise an error so you find out; ignore leaves it open for you to handle. Recent drivers close beforeunload prompts automatically.

With WebDriver BiDi enabled, the browsing context module also emits userPromptOpened and userPromptClosed events, so a test can react to a dialog the moment it appears rather than discovering it on the next command.

Switch to an alert, read its text, and close it:

        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        Assertions.assertEquals("cheese", alert.getText());
        alert.accept();
    wait = WebDriverWait(driver, timeout=2)
    alert = wait.until(EC.alert_is_present())
    text = alert.text
    alert.accept()
    alert = driver.switch_to.alert

    # Get the text of the alert
    alert.text

    # Press on Cancel button
    alert.dismiss
        let alert = await driver.switchTo().alert();
        let alertText = await alert.getText();
        await alert.accept();

Type into a prompt before accepting it:

        wait.until(ExpectedConditions.alertIsPresent());

        Alert alert = driver.switchTo().alert();
        Assertions.assertEquals("Enter something", alert.getText());

        alert.sendKeys("Selenium");
        alert.accept();
    wait = WebDriverWait(driver, timeout=2)
    alert = wait.until(EC.alert_is_present())
    alert.send_keys("Selenium")
    text = alert.text
    alert.accept()
    alert = driver.switch_to.alert

    # Type a message
    alert.send_keys('selenium')

    # Press on Ok button
    alert.accept
        let alert = await driver.switchTo().alert();
        //Type your message
        await alert.sendKeys(text);
        await alert.accept();

Decide up front what happens to an unexpected dialog:

	ChromeOptions chromeOptions = getDefaultChromeOptions();
	chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
    options = get_default_chrome_options()
    options.unhandled_prompt_behavior = 'accept'
    driver = webdriver.Chrome(options=options)
            var chromeOptions = new ChromeOptions();
            chromeOptions.UnhandledPromptBehavior = UnhandledPromptBehavior.DismissAndNotify;
      options = Selenium::WebDriver::Options.chrome
      options.unhandled_prompt_behavior = :accept

Common errors

  • NoAlertPresentException: you switched to an alert before it appeared, or the session’s prompt handler already closed it. Wait for it with alertIsPresent, and check unhandledPromptBehavior.
  • UnhandledAlertException: a dialog opened that nothing handled, and the driver closed it before running your command. Either handle the dialog in the test or change the prompt handler.

Reference