最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How can I check whether alert is present in selenium with zero implicit wait - Stack Overflow

programmeradmin4浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论