How to check if an alert exists using WebDriver? How to check if an alert exists using WebDriver? selenium selenium

How to check if an alert exists using WebDriver?


public boolean isAlertPresent() {     try     {         driver.switchTo().alert();         return true;     }   // try     catch (NoAlertPresentException Ex)     {         return false;     }   // catch }   // isAlertPresent()

check the link here https://groups.google.com/forum/?fromgroups#!topic/webdriver/1GaSXFK76zY


The following (C# implementation, but similar in Java) allows you to determine if there is an alert without exceptions and without creating the WebDriverWait object.

boolean isDialogPresent(WebDriver driver) {    IAlert alert = ExpectedConditions.AlertIsPresent().Invoke(driver);    return (alert != null);}


I would suggest to use ExpectedConditions and alertIsPresent(). ExpectedConditions is a wrapper class that implements useful conditions defined in ExpectedCondition interface.

WebDriverWait wait = new WebDriverWait(driver, 300 /*timeout in seconds*/);if(wait.until(ExpectedConditions.alertIsPresent())==null)    System.out.println("alert was not present");else    System.out.println("alert was present");