由于预期条件失败而面临错误:等待警报出现(以500毫秒间隔尝试5秒)

当条件下降时,转移到其他地方,但是当情况为真时,则不进入

try {
    //WebDriverWait wait = new WebDriverWait(driver,5);

    if(wait.until(ExpectedConditions.alertIsPresent()) == null) {
        System.out.println("alert was not present")
        (wait.until(ExpectedConditions.elementToBeclickable(By.xpath("/html/body/div[2]/div/div/div[2]/button[2]"))).click();
        Confirmation.click();
    }
    else {
        String Recipemessage = driver.switchTo().alert().getText();
        System.out.println(Recipemessage);

        if(Recipemessage.equals(alertmessage2) || Recipemessage.equals(alertmessage4) ) {

            Thread.sleep(3000);

            driver.switchTo().alert().accept();
            break;
        }
    }
} catch(Exception exp7) {
    System.out.println(exp7);
}
wcr997 回答:由于预期条件失败而面临错误:等待警报出现(以500毫秒间隔尝试5秒)

ExpectedConditions.alertIsPresent()永远不会返回null,它将返回警报或抛出TimeoutException

Alert alert = null;
try {
    alert = wait.until(ExpectedConditions.alertIsPresent());
catch(Exception) { }

if(alert == null) {
    System.out.println("alert was not present")
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[2]/div/div/div[2]/button[2]"))).click();
    Confirmation.click();
}
else {
    String Recipemessage = alert.getText();
    System.out.println(Recipemessage);

    if (Recipemessage.equals(alertmessage2) || Recipemessage.equals(alertmessage4)) {
        Thread.sleep(3000);
        alert.accept();
        break;
    }
}
,

wait.until将等待WebDriverWait中指定的时间,如果找不到警报,则会引发异常。因此,除了在if条件中使用它,您还可以像下面那样查找Alert是否存在。

            WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(10));
            bool AlertExists;
            try
            {
                wait.until(ExpectedConditions.alertIsPresent());
                AlertExists = true;
            }
            catch
            {
                AlertExists = false;
            }
            if(AlerExists)
            {
              //do stuff
            }
            else
            {
              //do other stuff
            }
本文链接:https://www.f2er.com/3161182.html

大家都在问