- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if an alert exists using WebDriver?
We can check if an alert exists with Selenium webdriver. An alert is created with the help of Javascript. We shall use the explicit wait concept in synchronization to verify the presence of an alert.
Let us consider the below alert and check its presence on the page. There is a condition called alertIsPresent which we will use to check for alerts. It shall wait for a specified amount of time for the alert after which it shall throw an exception.
We need to import org.openqa.selenium.support.ui.ExpectedConditions and import org.openqa.selenium.support.ui.WebDriverWait to incorporate expected conditions and WebDriverWait class.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; public class VerifyAlert{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String u ="https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm"driver.get(u); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // identify and click submit WebElement t = driver.findElement(By.name("submit")); t.click(); // Explicit wait condition for alert WebDriverWait w = new WebDriverWait(driver, 5); //alertIsPresent() condition applied if(w.until(ExpectedConditions.alertIsPresent())==null) System.out.println("Alert not exists"); else System.out.println("Alert exists"); driver.close(); } }
Output
- Related Articles
- Check if any alert exists using selenium with python.
- How to check if an element is visible with WebDriver?
- How to check if a file exists or not using Python?
- How to check if an item exists in a C# array?
- How to check if an item exists in a C# list collection?
- How to check if a MySQL database exists?
- How to check if dom has a class using WebDriver (Selenium 2)?
- How to check if a variable exists in JavaScript?
- How to check if a column exists in Pandas?
- How to check if a file exists in Golang?
- How to check if a value exists in an R data frame or not?
- Check if MongoDB database exists?
- How to check if value exists with MySQL SELECT 1?
- How to check if element exists in the visible DOM?
- How to check if event exists on element in jQuery?

Advertisements