How to Handle Alert in Selenium using Java?
Last Updated :
25 Feb, 2024
Imagine filling out a form online and accidentally missing some information. You only know if you made a mistake if the website tells you somehow, like with a pop-up message. This article explains what those pop-up messages are called in Selenium (alerts) and how to deal with them in your automated tests.
Prerequisites
- Eclipse IDE: Before downloading also make sure that your device has Java JDK. If you don’t have, to install Java refer to this: How to Download and Install Java for 64-bit machine?
- Install Eclipse IDE by referring to this article Eclipse IDE for Java Developers.
- Selenium: Download the Selenium latest stable version here.
- Web Driver: Download the Microsoft Edge Webdriver according to your version here.
Note:Â
To Open a Chrome Browser Using Selenium please refer to this article How to Open a Chrome Browser Using Selenium in Java?Â
What are Alerts in Selenium?
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.
Types of Alerts in Selenium
There are three types of Alert in Selenium, described as follows:
- Simple Alert
- Prompt Alert
- Confirmation Alert
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.
1. Simple Alert
The simple alert in selenium shows some information or warning on the window.
Â
2. Confirmation Alert
The confirmation alert asks for the permission to do some type of operations.
3. Prompt Alert
Prompt Alert asks some input from the user.
How to Handle 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.
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" );
|
Example of Alert Handling Using Selenium
- Launch the web browser and open the webpage “https://demoqa.com/alerts“
- Click on the confirmation alert button
- Accept the alert
- Click on the confirmation alert button again
- Reject the alert
Selenium test Script to Handle Alerts:
Java
package GFG_Maven.GFG_MAven;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class Geeks {
public static void main(String args[]) throws InterruptedException {
System.setProperty( "webdriver.chrome.driver" , "C:\\Users\\ADMIN\\Documents\\chromedriver.exe" );
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.findElement(By.xpath( "//*[@id=\"confirmButton\"]" )).click();
Thread.sleep( 3000 );
Alert alert = driver.switchTo().alert();
alert.accept();
driver.findElement(By.xpath( "//*[@id=\"confirmButton\"]" )).click();
Thread.sleep( 3000 );
Alert alert1 = driver.switchTo().alert();
alert1.dismiss();
}
}
|
Output:
The program will open the website and click on the confirmation alert button and accept the alert and again it click the alert button and decline the alert.
What are Popups in Selenium?
Selenium enables you to handle various types of popups and alerts that may appear during automated browsing, such as JavaScript alerts, modal dialogs, and browser notifications. You can switch to and interact with popup windows or accept/dismiss alerts using appropriate WebDriver methods.
How to Handle Popups in Selenium?
1. Switch to the Alert
Use driver.switchTo().alert()
. This tells Selenium to focus on the alert instead of the main page.
2. Interact with the Alert
- Get the text:Â UseÂ
alert.getText()
 to read the message displayed in the alert.
- Accept the alert:Â UseÂ
alert.accept()
 to click the “OK” button (or equivalent).
- Dismiss the alert:Â UseÂ
alert.dismiss()
 to click the “Cancel” button (or equivalent).
- Enter text (for prompts):Â UseÂ
alert.sendKeys("text")
 to input text in prompts before accepting or dismissing.
Example:
Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
public class AlertHandlingDemo {
public static void main(String[] args) {
System.setProperty( "webdriver.chrome.driver" , "D:\\driver\\Feb\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.findElement(By.id( "alertButton" )).click();
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println( "Alert text: " + alertText);
alert.accept();
driver.quit();
}
}
|
Handling Web Dialog Box/Popup Window using Selenium
1. Navigate to the webpage “https://demoqa.com/alerts“.
2. Locate and click the button that triggers the alert dialog.
3. Wait for the alert to be present using WebDriverWait.
4. Switch to the alert and print its text.
5. Accept the alert (click OK).
6. Close the browser.
Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HandleWebDialogBox {
public static void main(String[] args) {
System.setProperty( "webdriver.chrome.driver" , "D:\\driver\\Feb\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
WebElement clickMeButton = driver.findElement(By.id( "alertButton" ));
clickMeButton.click();
WebDriverWait wait = new WebDriverWait(driver, 10 );
wait.until(ExpectedConditions.alertIsPresent());
org.openqa.selenium.Alert alert = driver.switchTo().alert();
System.out.println( "Alert Text: " + alert.getText());
alert.accept();
driver.quit();
}
}
|
Conclusion
This article taught us how to deal with alerts and pop-ups when running automated tests using Selenium. Alerts are those small message boxes that show up on the screen and can be either for giving information or asking for user input. We learned different ways to handle these alerts, like accepting them, dismissing them, getting their text, and sending keys to them. Also, we explored how Selenium helps us manage different types of pop-ups and alerts that might come up during automated browsing. The main idea is that knowing how to handle alerts and pop-ups is crucial for making our automated tests strong and reliable.
Share your thoughts in the comments
Please Login to comment...