How to check if any Alert exists using Selenium with Java?
Last Updated :
03 Oct, 2024
Checking if an alert exists using Selenium with Java is a common scenario when testing web applications. Alerts are pop-up windows generated by the browser to notify users of important messages, such as errors, warnings, or confirmations. An alert window is a pop-up box that is shown over the webpage to convey error messages using the alert() function of JavaScript. A person might have to check the presence of such alert windows on the webpages for testing purposes.
In this tutorial, we will be using Selenium and Java to check if the alert window is present or not.
Different ways to check for Alert window
Multiple ways are used in which we can confirm the presence of an alert window on the webpage. Let us see these techniques and their working.
Note that before attempting to run these codes, you must either have created a Java project with build tools (Maven/Gradle) or created a simple Java project and download the Selenium JAR files from the internet. Also, you must have respective driver for your browser.
1) Using switchTo().alert() method
Java
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class App {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");//change the path manualy
WebDriver driver = new ChromeDriver();
driver.get("https://globbertrot.github.io/alertpage/");
if (isAlertPresent(driver)) {
System.out.println("Alert present");
} else {
System.out.println("No alert present");
}
driver.quit();
}
public static boolean isAlertPresent(WebDriver driver) {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
}
Explanation
- We have created a method named isAlertPresent() which takes the driver object as an argument.
- We are trying to change the context from the webpage to the alert window using switchTo().alert().
- If the alert window is not present, then switchTo().alert() throws NoAlertPresentException which we can catch to know alert window is not present.
2) Using UnhandledAlertException exception
Java
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class App {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");//change the path manualy
WebDriver driver = new ChromeDriver();
driver.get("https://globbertrot.github.io/alertpage/");
if (isAlertPresent(driver)) {
System.out.println("Alert present");
} else {
System.out.println("No alert present");
}
driver.quit();
}
public static boolean isAlertPresent(WebDriver driver) {
try {
driver.getTitle();
return false;
} catch (UnhandledAlertException e) {
return true;
}
}
}
Explanation
- We have created a method named isAlertPresent() which takes the driver object as an argument.
- We are trying to get the title of the webpage. Alternatively, we can perform any action on the webpage apart from getting title.
- If an alert is present on the webpage, then while trying to perform the above action, selenium will throw a UnhandledAlertException, suggesting to address the alert before performing any action on the webpage.
- We are catching this exception to check the presence of alert window.
3) Using ExpectedConditions.alertIsPresent() method
Java
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class App {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");//change the path manualy
WebDriver driver = new ChromeDriver();
driver.get("https://globbertrot.github.io/alertpage/");
if (isAlertPresent(driver)) {
System.out.println("Alert present");
} else {
System.out.println("No alert present");
}
driver.quit();
}
public static boolean isAlertPresent(WebDriver driver){
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(0L));
try {
wait.until(ExpectedConditions.alertIsPresent());
return true;
} catch (TimeoutException eTO) {
return false;
}
}
}
Explanation
- We have created a method named isAlertPresent() which takes the driver object as an argument.
- We are creating an explicit wait using WebDriverWait class.
- ExpectedConditions is a class containing various pre-defined conditions to be used along with a Wait object.
- One such method is alertIsPresent() which checks for the presence of alert window.
- Internally it uses switchTo().alert() method.
Output
Conclusion
In conclusion, handling alerts in Selenium using Java is an important skill for testers, as it allows the automation process to seamlessly manage pop-up alerts. By using methods like switchTo().alert()
, UnhandledAlertException
, or ExpectedConditions.alertIsPresent()
, you can efficiently check for and handle alerts during web application testing.
This ensures smoother testing, enabling you to bypass unnecessary interruptions and maintain test integrity.
Similar Reads
How to Handle Alert in Selenium using Java? 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
5 min read
How to check if an element exists with Selenium WebDriver? Selenium is one of the most popular tools for automating web applications. Selenium is not a single tool but rather a set of tools that helps QA testers and developers to automate web applications. It is widely used to automate user interactions on a web page like filling out web forms, clicking on
7 min read
How to check if an element exists with Python Selenium? Selenium is one of the most powerful and widely used tools for web automating web applications. Whether you're a software developer or a QA tester, Selenium is an important tool to have in your toolkit. Selenium is widely used for automating user interactions like clicking buttons, filling out the f
7 min read
How to check if an image is displayed on page using Selenium? When it comes to web automation and testing, It is important to confirm that a picture has been rendered on a particular page. This is because bad content loading is regarded as a big issue in the good user interface. Users can easily get frustrated with missing or broken images, hence the need for
3 min read
How to download File in Selenium Using java Automating the file download process is essential in web automation testing, especially for validating functionalities involving document downloads such as PDFs, images, or CSV files. However, Selenium WebDriver doesnât directly handle file downloads. To overcome this limitation, we can configure th
2 min read