等到页面加载完全使用硒3?

时间:2017-04-26 13:25:39

标签: javascript java selenium-webdriver selenium3

public boolean WaitForPageToLoad(){   

final ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
        public Boolean apply(final WebDriver driver) {
            return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
        }
    };

    final WebDriverWait wait = new WebDriverWait(this.driver, this.defaultTimeoutinSeconds);
    final boolean IsPageLoad = wait.until(pageLoadCondition);

    if (!IsPageLoad) {
        log.logInfo("Page doesn't load after " + this.defaultTimeoutinSeconds + " seconds");
    }
    return IsPageLoad;
}

上面的代码在selenium 2.53.1中工作但是当我升级到Selenium 3.1.X时,上面的代码不兼容。 Plaese任何人转换上面的代码,使其与硒3兼容。我得到低于错误
FluentWait类型中的(Function)方法不适用于参数(new ExpectedCondition(){})

1 个答案:

答案 0 :(得分:0)

此代码适用于Selenium3

driver = (new Driver(Driver.Browser.SAFARI)).getDriver();

driver.navigate().to("http://www.epochconverter.com/");

waitForLoad(driver);

static void  waitForLoad(WebDriver driver) {
    new WebDriverWait(driver, 50).until((ExpectedCondition<Boolean>) wd ->
    ((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));
}