How to check if an alert exists using WebDriver?

后端 未结 9 1741
名媛妹妹
名媛妹妹 2020-12-01 09:09

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

9条回答
  •  伪装坚强ぢ
    2020-12-01 09:33

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

提交回复
热议问题