VOOZH about

URL: https://www.geeksforgeeks.org/software-testing/how-to-handle-alert-in-selenium-using-java/

⇱ How to Handle Alert in Selenium using Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Handle Alert in Selenium using Java?

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:

  1. Simple Alert
  2. Prompt Alert
  3. Confirmation Alert

1. Simple Alert

The simple alert in selenium shows some information or warning on the window.

πŸ‘ Simple Alert

2. Confirmation Alert

The confirmation alert asks for the permission to do some type of operations.

πŸ‘ Confirmation alert

3. Prompt Alert

Prompt Alert asks some input from the user.

πŸ‘ Prompt Alert

Methods of Handing Alerts in Selenium

There 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.

2. void accept() 

The void accept method is used to click on the β€˜OK' button of the alert.

3. String getText() 

The void accept method is used to capture the alert message..

4. void sendKeys(String stringToSend)

It is used to send some data to the prompt alert.

Prerequisites

Before Handling alerts please download and make sure you have setup the below dependency for the automation of alerts.

  1. Java JDK, If you don’t have, to install Java refer to this: Download & Install Java.
  2. Install Eclipse IDE by referring to this article: Install Eclipse.
  3. Download the latest stable version of Selenium: Selenium Official Download Page.
  4. Download the Chrome WebDriver according to your version Here.
  5. Before handling alerts, you have to learn first to Open Chrome Browser Using Selenium.

Example of Alert Handling Using Selenium

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

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:

  1. Simple Alert: This alert shows a message and has only an "OK" button. We click the button to trigger the alert, then switch to the alert, verify its message, and accept it.
  2. Confirmation Alert: This alert asks a user to confirm or cancel an action, with "OK" and "Cancel" options. We click the button to open the confirmation alert, verify its message, and dismiss the alert (selecting "Cancel").
  3. Prompt Alert: This alert asks the user to enter some text. We click the button to open the prompt alert, enter some text in the input field, and accept the alert to submit the text.

AlertTest.java

Output

πŸ‘ Output-of-Handling-Alerts
Output of Handling Alerts

Handling Alert GetText and Validation

In 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


πŸ‘ Output-of-Get-Text-in-selenium-WebDriver
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.

Comment
Article Tags:

Explore