Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

What is Alert in Selenium?

An Alert in Selenium refers to a browser pop-up that appears as a notification or prompt, requiring user interaction. Selenium WebDriver provides methods to handle these alerts, which are classified into three types:

Types of Alerts in Selenium:

  • Simple Alert - Displays a message with an "OK" button.
  • Confirmation Alert - Asks for confirmation with "OK" and "Cancel" buttons.
  • Prompt Alert - Contains a text box for user input along with "OK" and "Cancel" buttons.

Handling Alerts in Selenium:

  • Accept the alert → Clicks "OK".
  • Dismiss the alert → Clicks "Cancel".
  • Get text from the alert → Retrieves the message displayed.
  • Send input to a prompt alert → Enters text into a text box (for prompt alerts only).

Important Note: If an operation tries to interact with an alert that is not present or has already been closed in the browser, a NoAlertPresentException is thrown.

Why Handle Alerts in Selenium?

  • Prevents test execution from being blocked (alerts must be handled before proceeding).
  • Automates user interactions that involve pop-ups.
  • Validates alert messages in test cases.

Use Cases

  • Handling browser security pop-ups.
  • Managing JavaScript alerts triggered by form validations.
  • Testing user interactions with confirmation dialogs.

Alerts are common in web applications for notifications, confirmations, and user inputs. Selenium’s alert handling methods ensure smooth automation by interacting with these pop-ups seamlessly.

Test your website on 3000+ browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free