暂停/继续在webdriver中执行selenium脚本

时间:2015-05-15 19:24:50

标签: java selenium-webdriver

我的项目中有大量流量。我使用硒自动化这些流程如果我在第99步中得到任何错误,包括100步,那么我需要从第一步开始。

即使找不到定位器,也有任何暂停/继续selenium脚本的事情。

1 个答案:

答案 0 :(得分:0)

public void waitForElementToBeVisible(String cssSelector) throws Throwable {
    try {
        WebDriverWait wait = new WebDriverWait(driver, 30); //30 = TIMEOUT DURATION IN SECONDS
        wait.until(ExpectedConditions.or(
                ExpectedConditions.visibilityOfElementLocated(By.cssSelector(cssSelector)) //CSS SELECTOR CAN BE DEFINED WHEN THIS METHOD IS CALLED e.g. waitForElementToBeVisible("Button");
        ));
    } catch (Exception e) {
        System.out.println("Timeout exceeded");
        //ACTIONS YOU WANT TO CARRY OUT IF THE ELEMENT CAN'T BE FOUND, COULD JUST BE NOTHING.
    }
}

或者,如果你想在你的测试中运行它,而不设置方法,只需执行以下操作....

try {
    WebDriverWait wait = new WebDriverWait(driver, 30); //30 = TIMEOUT DURATION IN SECONDS
    wait.until(ExpectedConditions.or(
            ExpectedConditions.visibilityOfElementLocated(By.cssSelector("Button")) //CSS SELECTOR CAN BE DEFINED WHEN THIS METHOD IS CALLED e.g. waitForElementToBeVisible("Button");
    ));
} catch (Exception e) {
    System.out.println("Timeout exceeded");
    //ACTIONS YOU WANT TO CARRY OUT IF THE ELEMENT CAN'T BE FOUND, COULD JUST BE NOTHING.
}