admin管理员组

文章数量:1410723

at few places, am facing some unhandled alerts, so for every click planning to check if alert is present, for that am using the following code,

public boolean isAlertPresent(){
    try{
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
        Alert a=driver.switchTo().alert();
        a.accept();
        return true;
    }
    catch (NoAlertPresentException e) {
        return false;
    }
    finally{
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
}    

but the above code is taking some time to check whether alert is present, as I am going to use this method for every click, its too costly to wait, making the implicit wait zero in the above code has no effect. can anybody help on this.

at few places, am facing some unhandled alerts, so for every click planning to check if alert is present, for that am using the following code,

public boolean isAlertPresent(){
    try{
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
        Alert a=driver.switchTo().alert();
        a.accept();
        return true;
    }
    catch (NoAlertPresentException e) {
        return false;
    }
    finally{
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
}    

but the above code is taking some time to check whether alert is present, as I am going to use this method for every click, its too costly to wait, making the implicit wait zero in the above code has no effect. can anybody help on this.

Share Improve this question asked Nov 22, 2014 at 6:11 Muthu KumarMuthu Kumar 4901 gold badge6 silver badges8 bronze badges 3
  • possible duplicate of How to wait for an alert in Selenium webdriver ? – alecxe Commented Nov 22, 2014 at 6:12
  • In a "possible duplicate" question there is an answer that might fit your needs - it is following the EAFP approach: try to switch and handle the exception, repeat. – alecxe Commented Nov 22, 2014 at 6:14
  • @alecxe my need is somewhat different than this, my problem is very specific about checking for an alert without any implicit wait. Say for example, if make implicit wait zero and check for an element is displayed, it will give result immediately, but while checking alert, that is not the case, even after making implicit wait to zero, it is waiting for sometime. – Muthu Kumar Commented Nov 22, 2014 at 6:20
Add a ment  | 

1 Answer 1

Reset to default 4

Implicit waits are for assigning global timeout(s), that means once assigned, selenium will wait for that amount of time (at max), each time it tries to find an element. On the other hand, Explicit waits are useful in assigning timeouts exclusively to any element in the webpage, and it overrides the timeout so set by implicit wait.

So, for the above, you can try Explicit wait instead like this:

public boolean isAlertPresent(){ 
    try{ 
        Alert a = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
        if(a!=null){
            System.out.println("Alert is present");
            driver.switchTo().alert().accept();
            return true;
        }else{
            throw new Throwable();
        }
    } 
    catch (Throwable e) {
        System.err.println("Alert isn't present!!");
        return false; 
    } 

} 

This will check the existence of alert within 10 seconds. If it finds the alert within that, it will return 'true' else it will return 'false'.

本文标签: javaHow can I check whether alert is present in selenium with zero implicit waitStack Overflow