Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:SeleniumHQ Selenium UnexpectedAlertBehaviour

From Leeroopedia
Knowledge Sources
Domains WebDriver, Browser_Automation
Last Updated 2026-02-12 00:00 GMT

Overview

Concrete tool for configuring how the browser handles unexpected JavaScript alerts during WebDriver commands, provided by the Selenium WebDriver library.

Description

UnexpectedAlertBehaviour is a Java enum in the org.openqa.selenium package that defines five strategies for handling JavaScript alert, confirm, and prompt dialogs that appear unexpectedly during WebDriver operations. These strategies map directly to the W3C WebDriver specification's "unhandled prompt behavior" capability:

  • ACCEPT ("accept") -- Automatically accept (click OK on) any unexpected alert.
  • DISMISS ("dismiss") -- Automatically dismiss (click Cancel on) any unexpected alert.
  • ACCEPT_AND_NOTIFY ("accept and notify") -- Accept the alert and throw an UnhandledAlertException to notify the caller.
  • DISMISS_AND_NOTIFY ("dismiss and notify") -- Dismiss the alert and throw an UnhandledAlertException to notify the caller.
  • IGNORE ("ignore") -- Do not handle the alert; leave it open, which may cause subsequent commands to fail.

Each enum constant wraps a string value matching the W3C wire protocol representation. The enum provides a case-insensitive fromString factory method and overrides toString() to return the wire-protocol string.

Usage

Use UnexpectedAlertBehaviour when configuring browser capabilities to control how the WebDriver session responds to alerts that appear during command execution:

  • Use ACCEPT or DISMISS for fully automated flows where alerts should be handled silently.
  • Use ACCEPT_AND_NOTIFY or DISMISS_AND_NOTIFY when you want automatic handling but still need to know when alerts occur.
  • Use IGNORE when you plan to explicitly handle all alerts in your test code.

Code Reference

Source Location

Signature

public enum UnexpectedAlertBehaviour {
    ACCEPT("accept"),
    DISMISS("dismiss"),
    ACCEPT_AND_NOTIFY("accept and notify"),
    DISMISS_AND_NOTIFY("dismiss and notify"),
    IGNORE("ignore");

    UnexpectedAlertBehaviour(String text)
    public String toString()
    public static UnexpectedAlertBehaviour fromString(String text)
}

Import

import org.openqa.selenium.UnexpectedAlertBehaviour;

I/O Contract

Enum Constants
Constant Wire Value Description
ACCEPT "accept" Silently accept unexpected alerts
DISMISS "dismiss" Silently dismiss unexpected alerts
ACCEPT_AND_NOTIFY "accept and notify" Accept the alert and raise an UnhandledAlertException
DISMISS_AND_NOTIFY "dismiss and notify" Dismiss the alert and raise an UnhandledAlertException
IGNORE "ignore" Leave the alert open; do not handle it
Methods
Method Input Output Description
toString() none String -- wire-protocol value Returns the string representation of the behaviour
fromString(String text) String (nullable, case-insensitive) UnexpectedAlertBehaviour or null Looks up the enum constant matching the given string; returns null if no match or input is null

Usage Examples

// Automatically dismiss unexpected alerts
ChromeOptions options = new ChromeOptions();
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS);
WebDriver driver = new ChromeDriver(options);

// Accept alerts but still get notified via exceptions
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT_AND_NOTIFY);
WebDriver firefoxDriver = new FirefoxDriver(firefoxOptions);

// Convert from a configuration string
UnexpectedAlertBehaviour behaviour = UnexpectedAlertBehaviour.fromString("dismiss and notify");

// Get the wire-protocol string
String wireValue = UnexpectedAlertBehaviour.ACCEPT.toString(); // returns "accept"

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment