在WebDriverWait中继续Selenium Test而不是Timeout

时间:2016-01-10 11:27:30

标签: java selenium selenium-webdriver wait

我在Selenium中使用WebDriverWait,我想等待10秒等待,如果出现一些警告或确认框,如果它出现我将接受它。例如,如果确认框要求我"离开Page"或者"留在页面"然后我想自动离开页面,如果10秒内没有出现警报,则继续执行代码Excecution而不是抛出异常。或者你可以告诉我一些方法来接受所有确认,在整个硒测试期间自动提示框。谢谢 这是我的代码

WebDriver driver = new FirefoxDriver();  
    driver.get("https:www.google.com");
    WebDriverWait wait = new WebDriverWait(driver, 10 );
    if(wait.until(ExpectedConditions.alertIsPresent())==null)
    {
    System.out.println("alert was not present");}
    else{
    Alert alert = driver.switchTo().alert(); 
    alert.accept();
    System.out.println("alert was present");}

1 个答案:

答案 0 :(得分:0)

try / catch对于捕获/处理异常非常有用。只需在try块中放置警报代码,如果发生任何异常,它将在catch块中处理并在执行后继续...

WebDriver driver = new FirefoxDriver();  
    driver.get("https:www.google.com");
    WebDriverWait wait = new WebDriverWait(driver, 5 );

    try{

    if(wait.until(ExpectedConditions.alertIsPresent())==null)
    {
    System.out.println("alert was not present");
    }else{
        Alert alert = driver.switchTo().alert(); 
        alert.accept();
        System.out.println("alert was present");
        }

    }catch(Exception e){
        System.out.println("ignored alret not present exception" +e.getMessage());
    }


}

由于