Webdriverwait未按预期工作

时间:2016-12-19 07:13:11

标签: java selenium selenium-webdriver

似乎我的代码是正确的但是没有按预期工作,Webdriver在我的代码中并没有真正起作用。我希望在返回上一页之前先加载页面。以下是我的代码。

Browser.clickElement(MenuButton, "XPath", _driver);

        Browser.clickElement(Menus[i], "XPath", _driver);

        for(int j=0; j<1;  j++) //StrategySubLinks.length;
        {
          Browser.clickElement(StrategySubLinks[j], "XPath", _driver);
          //wait for a certain element to be visible before going back to the previous page
          //wait for the footer link
          WebDriverWait wait = new WebDriverWait(_driver, 5);
          wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"asdfdasf-asdfasdfcookie\"]/p/a[2]")));



          //go back to previous page
          //_driver.navigate().back();
          _driver.navigate().refresh();
          JavascriptExecutor js = (JavascriptExecutor) _driver;
          js.executeScript("window.history.back();");

2 个答案:

答案 0 :(得分:1)

尝试以下:

WebDriverWait wait = new WebDriverWait(dr, 30);
wait.until(ExpectedConditions.jsReturnsValue("return document.readyState==\"complete\";"));

void waitForLoad(WebDriver driver) {
    ExpectedCondition<Boolean> pageLoadCondition = new
        ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
        };
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(pageLoadCondition);
}

答案 1 :(得分:0)

新版本的selenium不稳定,我使用的旧版本的selenium是2.53,因此代码webdriverwait将按预期工作。