Wait for page load in Selenium

2018-12-31 15:29发布

How do you make Selenium 2.0 wait for the page to load?

30条回答
浮光初槿花落
2楼-- · 2018-12-31 16:03
driver.asserts().assertElementFound("Page was not loaded",
By.xpath("//div[@id='actionsContainer']"),Constants.LOOKUP_TIMEOUT);
查看更多
美炸的是我
3楼-- · 2018-12-31 16:04

Call below Function

public static boolean isloadComplete(WebDriver driver)
{
    return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("loaded")
            || ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
查看更多
闭嘴吧你
4楼-- · 2018-12-31 16:08

Explicitly wait or conditional wait in this wait until given this condition.

WebDriverWait wait = new WebDriverWait(wb, 60);
wait.until(ExpectedConditions.elementToBeClickable(By.name("value")));

This will wait for every web element for 60 seconds.

Use implicitly wait for wait of every element on page till that given time.

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

This will wait for every web element for 60 seconds.

查看更多
只若初见
5楼-- · 2018-12-31 16:08

The easiest way is just wait for some element which will appear on loaded page.

If you would like to click on some button already after page is loaded you could use await and click:

await().until().at.most(20, TimeUnit.Seconds).some_element.isDisplayed(); // or another condition
getDriver().find(some_element).click;
查看更多
深知你不懂我心
6楼-- · 2018-12-31 16:10
/**
 * Call this method before an event that will change the page.
 */
private void beforePageLoad() {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("document.mpPageReloaded='notYet';");
}

/**
 * Call this method after an event that will change the page.
 * 
 * @see #beforePageLoad
 * 
 *      Waits for the previous page to disappear.
 */
private void afterPageLoad() throws Exception {
    (new WebDriverWait(driver, 10)).until(new Predicate<WebDriver>() {

        @Override
        public boolean apply(WebDriver driver) {
            JavascriptExecutor js = (JavascriptExecutor) driver;
            Object obj = js.executeScript("return document.mpPageReloaded;");
            if (obj == null) {
                return true;
            }
            String str = (String) obj;
            if (!str.equals("notYet")) {
                return true;
            }
            return false;
        }
    });
}

You can change from the document to an element, in the case of where only part of a document is being changed.

This technique was inspired by the answer from sincebasic.

查看更多
弹指情弦暗扣
7楼-- · 2018-12-31 16:13

You may remove the System.out line. It is added for debug purposes.

WebDriver driver_;

public void waitForPageLoad() {

    Wait<WebDriver> wait = new WebDriverWait(driver_, 30);
    wait.until(new Function<WebDriver, Boolean>() {
        public Boolean apply(WebDriver driver) {
            System.out.println("Current Window State       : "
                + String.valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState")));
            return String
                .valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState"))
                .equals("complete");
        }
    });
}
查看更多
登录 后发表回答