SeleniumHQ/selenium · error · UnexpectedAlertOpenError

${message}

Error message

${message}

What it means

Thrown by checkLegacyResponse() in error.js when a legacy Selenium 2.0 wire protocol response has a non-zero numeric `status` and the `value` is an object containing a `message`. The error constructor is resolved from LEGACY_ERROR_CODE_TO_TYPE; for UnexpectedAlertOpenError it also extracts the alert text. The message comes from value.message.

Source

Thrown at javascript/selenium-webdriver/lib/error.js:560

  // Handle the legacy Selenium error response format.
  if (isObject(responseObj) && typeof responseObj.status === 'number' && responseObj.status !== 0) {
    const { status, value } = responseObj

    let ctor = LEGACY_ERROR_CODE_TO_TYPE.get(status) || WebDriverError

    if (!value || typeof value !== 'object') {
      throw new ctor(value + '')
    } else {
      let message = value['message'] + ''
      if (ctor !== UnexpectedAlertOpenError) {
        throw new ctor(message)
      }

      let text = ''
      if (value['alert'] && typeof value['alert']['text'] === 'string') {
        text = value['alert']['text']
      }
      throw new UnexpectedAlertOpenError(message, text)
    }
  }
  return responseObj
}

// PUBLIC API

module.exports = {
  ErrorCode,

  WebDriverError,
  DetachedShadowRootError,
  ElementClickInterceptedError,
  ElementNotInteractableError,
  ElementNotSelectableError,
  InsecureCertificateError,
  InvalidArgumentError,
  InvalidCookieDomainError,

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Upgrade to a W3C-compliant Selenium server/Grid (Selenium 3.141+ in W3C mode or Selenium 4+).
  2. Handle unexpected alerts proactively using the Alert API: driver.switchTo().alert().dismiss().
  3. Read value.message and value.alert.text in the caught error for root-cause diagnosis.
  4. Set pageLoadStrategy or unexpectedAlertBehavior capability to control alert handling.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  // ... execute command ...
} catch (e) {
  if (e instanceof UnexpectedAlertOpenError) {
    console.warn('Unexpected alert:', e.text || '(no text)')
    await driver.switchTo().alert().dismiss()
    // retry the command if appropriate
  }
  throw e
}

Prevention

When it happens

Trigger: Connecting to a legacy Selenium 2 server/endpoint that returns {status, value:{message,...}} format. An unexpected JavaScript alert is open when a command executes, and the server uses the legacy protocol. A third-party service using the legacy wire protocol returns an error.

Common situations: Using old Selenium server/Grid versions (pre-W3C); third-party testing services with legacy protocol support; tests that trigger unexpected browser alerts; mixed protocol environments.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/c2073ac3e2eb9ba3. Report an issue: GitHub.