Last Updated : 28 Jul, 2025
In Selenium WebDriver, handling alerts is a common requirement for automating interactions with web applications. An Alert is nothing but a small message box that appears on the screen to give some kind of information and give a warning for a potentially damaging operation or permission to perform that operation.
These alerts classified into three types:
The simple alert in selenium shows some information or warning on the window.
2. Confirmation AlertThe confirmation alert asks for the permission to do some type of operations.
3. Prompt AlertPrompt Alert asks some input from the user.
Methods of Handing Alerts in SeleniumThere are the four methods that we would be using along with the Alert interface.
1. void dismiss()The void dismiss method is used to click on the ‘Cancel’ button of the alert.
Java
driver.switchTo().alert().dismiss();
2. void accept()
The void accept method is used to click on the ‘OK' button of the alert.
Java
driver.switchTo().alert().accept();
3. String getText()
The void accept method is used to capture the alert message..
Java
driver.switchTo().alert().getText();
4. void sendKeys(String stringToSend)
It is used to send some data to the prompt alert.
Java
driver.switchTo().alert().sendKeys("Text");
Prerequisites
Before Handling alerts please download and make sure you have setup the below dependency for the automation of alerts.
let's first set up a basic Selenium test environment with Java. Below is a basic structure of how to set up a WebDriver instance for running the tests.
BaseTest.java
Java
package io.learn;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
public class BaseTest {
protected WebDriver driver;
// Set up the ChromeDriver
@BeforeMethod
public void setup() {
// Set the path to your chromedriver executable
System.setProperty("webdriver.chrome.driver", "C:\\Users\\path of the chromedriver\\drivers\\chromedriver.exe");
// Initialize the ChromeDriver
driver = new ChromeDriver();
}
// Close the browser after each test
@AfterMethod
public void teardown() {
if (driver != null) {
driver.quit();
}
}
}
In BaseTest setup, the ChromeDriver is initialized, and the WebDriver is configured to open and close the browser automatically for each test.
Create class AlertTest.java
, and combined the three types of alerts:
AlertTest.java
package io.learn.alerts;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.learn.BaseTest;
public class AlertTest extends BaseTest {
String URl1 = "https://bonigarcia.dev/selenium-webdriver-java/dialog-boxes.html";
@Test
void testAlert() {
driver.get(URl1);
driver.findElement(By.id("my-alert")).click(); // Triggering simple alert
// Handling the alert
Alert alert = driver.switchTo().alert();
Assert.assertEquals(alert.getText(), "Hello world!");
alert.accept(); // Accept the alert (click OK)
System.out.println("Simple Alert Executed Successfully");
}
@Test
void testConfirm() {
driver.get(URl1);
driver.findElement(By.id("my-confirm")).click(); // Triggering confirmation alert
// Handling the confirmation alert
Alert confirm = driver.switchTo().alert();
Assert.assertEquals(confirm.getText(), "Is this correct?");
confirm.dismiss(); // Dismiss the alert (click Cancel)
System.out.println("Confirmation Alert Executed Successfully");
}
@Test
public void testPromptAlert() {
driver.get(URl1);
driver.findElement(By.id("my-prompt")).click(); // Triggering prompt alert
// Handling the prompt alert
Alert prompt = driver.switchTo().alert();
prompt.sendKeys("vaibhav"); // Enter text in the prompt
prompt.accept(); // Accept the prompt (click OK)
System.out.println("Prompt Alert Executed Successfully");
}
}
Output
Output of Handling Alerts Handling Alert GetText and ValidationIn many scenarios, you may need to get the message of an alert to perform some validation. Selenium provides the getText()
method for this purpose.
AlertGetText.java
Java
package io.learn.alerts;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.learn.BaseTest;
public class AlertGetText extends BaseTest {
@Test
void testConfirm() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/dialog-boxes.html");
driver.findElement(By.id("my-confirm")).click(); // Clicking the confirmation alert button
// Switch to the confirmation alert
Alert confirm = driver.switchTo().alert();
// Get and validate the text of the alert
Assert.assertEquals(confirm.getText(), "Is this correct?");
System.out.println(confirm.getText());
// Dismiss the alert (clicking "Cancel")
confirm.dismiss();
System.out.println("Confirmation Alert Executed Successfully");
}
}
Output of Get Text in selenium WebDriver
Handling alerts is a fundamental skill for any Selenium WebDriver user. Whether it's a simple message alert, a confirmation alert, or a prompt requiring user input, Selenium offers straightforward methods to interact with them effectively in the automation testing.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4