Selenium Webdriver Alert | 1. Simple Alert | 2. Confirmation Alert | 3. Prompt Alert

Before understanding Selenium Webdriver Alert, let’s understand what is alert. At times, web pages use dialog windows to inform us about some outcomes of any operation or seek confirmation by us. These dialogs overlay the current content, pausing other actions until user dismiss it. We call it “alerts,” these dialog boxes lack customization and adopt the default browser appearance.

This article covers native alerts, important alert types, and comprehensive guidance on interacting with them using Selenium WebDriver in Java. However, before exploring Selenium WebDriver, let’s delve deeper into these dialog windows.

What is JavaScript Alerts?

JavaScript alerts, also known as Native alerts, are directly supported by browsers via JavaScript. They are small message dialogs that pop up on the browser window. Typically, alerts serve to notify users of information, warnings, request input, or seek confirmation. When a native alert appears, it redirects user focus from the current web page and it requires a response from user. Consequently, these alerts restrict access to other sections of the webpage until the required action within the alert is taken.

Types of Alerts?

An alert is a pop-up dialog box commonly used in web applications to convey important information to the user or request confirmation for an action. It can be triggered by the website’s code using JavaScript functions like alert(), confirm(), or prompt().

  • Simple Alert: A simple pop-up box that displays a message and an “OK” button. It’s commonly used to display information or notifications to the user.
    • Code to generate Simple Alert: driver.execute_script(“alert(‘This is a simple alert’);”)
  • Confirmation Alert: Similar to an alert but includes two buttons – “OK” and “Cancel”. It’s used when the user needs to confirm or cancel an action.
    • Code to generate Confirmation Alert: driver.execute_script(“confirm(‘Do you want to proceed?’);”)
  • Prompt Alert: It displays a message along with an input field for the user to enter text. It typically has “OK” and “Cancel” buttons and is used to receive user input.
    • Code to generate Prompt Alert: driver.execute_script(“prompt(‘Please enter your name:’);”)

In Selenium WebDriver, handling alerts involves switching the WebDriver focus to the alert dialog and performing actions like accepting, dismissing, or interacting with its content based on the type of alert and the desired actions specified by your test scenario.

How To Interact With Alerts?

Interacting with alerts in Selenium WebDriver involves switching the WebDriver’s focus to the alert, then performing actions based on the alert type. Here’s a breakdown of handling various types of alerts using Java:

  1. To switch from main Window to required alert: driver.switchTo().alert();
  2. To get text on the alert: String alertText = simpleAlert.getText();
  3. Accepts the alert by clicking the ‘OK’ button: simpleAlert.accept();
  4. Dismiss the alert by clicking the ‘Cancel’ button: confirmationAlert.dismiss();
  5. It send a String to the prompt alert : promptAlert.sendKeys("Your Name");

Simple Alert (OK button only):

WebDriver driver = new ChromeDriver();

((JavascriptExecutor) driver).executeScript(“alert(‘This is a simple alert’);”);

Alert simpleAlert = driver.switchTo().alert();

String alertText = simpleAlert.getText();
System.out.println(“Alert Text: ” + alertText);



Confirmation Alert (OK and Cancel buttons):

WebDriver driver = new ChromeDriver();

((JavascriptExecutor) driver).executeScript(“confirm(‘Do you want to proceed?’);”);

Alert confirmationAlert = driver.switchTo().alert();

String alertText = confirmationAlert.getText();
System.out.println(“Alert Text: ” + alertText);



Prompt Alert (Input field with OK and Cancel buttons):

WebDriver driver = new ChromeDriver();

((JavascriptExecutor) driver).executeScript(“prompt(‘Please enter your name:’);”);

Alert promptAlert = driver.switchTo().alert();

promptAlert.sendKeys(“Your Name”);



Selenium Webdriver alert; alerts in selenium; simple alert; selenium alert

Leave a Reply