90

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.

Liam
  • 27,717
  • 28
  • 128
  • 190
Yunfei Gu
  • 921
  • 1
  • 6
  • 3

9 Answers9

93
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

ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40
  • 2
    In the link you can see hos to accept or dismiss the alert window – VinnyG Dec 05 '12 at 00:01
  • 3
    The slightly wrong answer below and the one on http://stackoverflow.com/questions/8244723/alert-handling-in-selenium-2-webdriver-with-java are a much better way to do that. The try/catch model, as well as being clunky, logs a message about there not being an alert. – Don Willis Jun 07 '13 at 01:04
  • 4
    `ExpectedConditions.alertIsPresent()` gives you exactly the same thing, but in a nicer way and in just one line :) – nilesh Feb 12 '14 at 14:30
  • ExpectedConditions doesn't save much more code then the simple try catch. – sinisterrook Mar 13 '15 at 14:02
  • 1
    One issue with this approach is that while checking to see if the alert exists, the context has been switched to the alert. This might be problematic if you aren't expecting it. – JeffC Jun 20 '17 at 17:05
31

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);
}
Lee Jensen
  • 2,151
  • 2
  • 21
  • 23
13

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");
Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
nilesh
  • 14,131
  • 7
  • 65
  • 79
9

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).

andyf
  • 3,262
  • 3
  • 23
  • 37
  • When I call a C# implementation of your function, it throws the exception, but it also closes the alert. – RMK Feb 12 '15 at 20:03
  • 1
    despite that it also closes the alert, so far I found that this approach is fastest when dealing with alert detection, even faster than `ExpectedConditions.alertIsPresent` – Thariq Nugrohotomo Apr 04 '16 at 06:21
  • The main problem with this approach is it eating the alert. When alert is not there driver.switchTo().alert() it taking around 6-10 ms in FF 62 – Pranoy Sarkar Sep 25 '18 at 13:19
8

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.

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
  • Another note: In C#, this is the WebDriverTimeoutException. I suppose that is because there is a System.TimeoutException class that could have easily gotten mixed up with that. – John Chesshir Mar 12 '18 at 22:05
3

ExpectedConditions is obsolete, so:

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

C# Selenium 'ExpectedConditions is obsolete'

CarolCiola
  • 111
  • 10
  • I wish they would add an option to the DefaultWait, so we can disable the Exception on a Timeout event. I had to make my own override of this class, so I can disable it, otherwise I have to put yet another Try/Catch block around the await and it's just soo many try/catchs everywhere, already, didn't want to add one more just to silence the timeout, also it slows down the bot with the Throw. – Zorkind Oct 28 '22 at 20:33
2
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;
      }
}
j.barrio
  • 1,006
  • 1
  • 13
  • 37
Naveen
  • 31
  • 6
2

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");
    }
    }
Vishnu B S
  • 21
  • 4
1

public boolean isAlertPresent() {

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

}

Vikas
  • 61
  • 1
  • 1