How to check if an alert exists using WebDriver?

SeleniumWebdriverAlert

Selenium Problem Overview


I need to check the existence of Alert in WebDriver.

Sometimes it pops up an alert but sometimes it will not pop up. I need to check if the alert exists first, then I can accept or dismiss it or it will say: no alert found.

Selenium Solutions


Solution 1 - Selenium

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

Solution 2 - Selenium

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);
}

Solution 3 - Selenium

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");

Solution 4 - Selenium

I found catching exception of driver.switchTo().alert(); is so slow in Firefox (FF V20 & selenium-java-2.32.0).`

So I choose another way:

	private static boolean isDialogPresent(WebDriver driver) {
		try {
			driver.getTitle();
			return false;
		} catch (UnhandledAlertException e) {
			// Modal dialog showed
			return true;
		}
	}

And it's a better way when most of your test cases is NO dialog present (throwing exception is expensive).

Solution 5 - Selenium

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

public boolean isAlertPresent(){
	boolean foundAlert = false;
	WebDriverWait wait = new WebDriverWait(driver, 0 /*timeout in seconds*/);
	try {
		wait.until(ExpectedConditions.alertIsPresent());
		foundAlert = true;
	} catch (TimeoutException eTO) {
		foundAlert = false;
	}
	return foundAlert;
}

Note: this is based on the answer by nilesh, but adapted to catch the TimeoutException which is thrown by the wait.until() method.

Solution 6 - Selenium

ExpectedConditions is obsolete, so:

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
        wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());

https://stackoverflow.com/questions/49866334/c-sharp-selenium-expectedconditions-is-obsolete][1]

Solution 7 - Selenium

public static void handleAlert(){
	if(isAlertPresent()){
	    Alert alert = driver.switchTo().alert();
		System.out.println(alert.getText());
		alert.accept();
	}
}
public static boolean isAlertPresent(){
	  try{
		  driver.switchTo().alert();
	      return true;
	  }catch(NoAlertPresentException ex){
	  	  return false;
	  }
}

Solution 8 - Selenium

This code will check whether the alert is present or not.

public static void isAlertPresent(){
	try{
	Alert alert = driver.switchTo().alert();
	System.out.println(alert.getText()+" Alert is Displayed"); 
	}
	catch(NoAlertPresentException ex){
	System.out.println("Alert is NOT Displayed");
	}
	}

Solution 9 - Selenium

public boolean isAlertPresent() {

try 
{ 
    driver.switchTo().alert(); 
    system.out.println(" Alert Present");
}  
catch (NoAlertPresentException e) 
{ 
    system.out.println("No Alert Present");
}    

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYunfei GuView Question on Stackoverflow
Solution 1 - SeleniumManMohan VyasView Answer on Stackoverflow
Solution 2 - SeleniumLee JensenView Answer on Stackoverflow
Solution 3 - SeleniumnileshView Answer on Stackoverflow
Solution 4 - SeleniumandyfView Answer on Stackoverflow
Solution 5 - SeleniumVince BowdrenView Answer on Stackoverflow
Solution 6 - SeleniumCarolCiolaView Answer on Stackoverflow
Solution 7 - SeleniumNaveenView Answer on Stackoverflow
Solution 8 - SeleniumVishnu B SView Answer on Stackoverflow
Solution 9 - SeleniumVikasView Answer on Stackoverflow