ryskajakub
ryskajakub

Reputation: 6431

Selenium driver - wait on page reload

Is there a way, how to wait on page reload? For example, when I'm on page localhost:9000/web and I instruct the webdriver again to navigate to the localhost:9000/web. I don't want or can't indicate the reloading by waiting on some elements.

Upvotes: 7

Views: 19899

Answers (5)

Jlearner
Jlearner

Reputation: 613

We can set pageLoadTimeout, once set it wil be there throught the webdriver session, and if the exception is thrown because of timeout then we can't restore same session so need to create new instance.

        WebDriver driver = new FirefoxDriver();

        //waits 10 seconds for page to load
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        try {
            driver.get("https://yourdomain.com");
        } catch (TimeoutException e) {
            driver.close();
            driver.quit();

            //create new instance of webdriver
            driver = new FirefoxDriver();

            //waits 5 minutes for page to load
            driver.manage().timeouts().pageLoadTimeout(300, TimeUnit.SECONDS);
            driver.get("https://yourdomain.com");
        }

Upvotes: 0

pokeymond
pokeymond

Reputation: 671

This is old but I also wanted a solution for this, and stumbled onto this question.

Thanks to the answers posted, I created my own solution by combining the expected condition of staleness of the html and the waitForPageToLoad function from @mfsi_sitamj post.

Something like this:

@CacheLookup
@FindBy(tagName = "html")
private WebElement __document;

public void waitForReload() {
    Wait<WebDriver> wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.stalenessOf(this.__document));
    wait.until((ExpectedCondition<Boolean>) wd ->
        ((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));
}

Upvotes: 3

Sitam Jana
Sitam Jana

Reputation: 3129

In Selenium WebDriver we can implement waitForPageToLoad using the following code snippet :

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

    Wait < WebDriver > wait = new WebDriverWait(driver, 60);
    try {
        wait.until(pageLoad);
    } catch (Throwable pageLoadWaitError) {
        assertFalse("Timeout during page load", true);
    }
}

Upvotes: 6

Sitam Jana
Sitam Jana

Reputation: 3129

Webdriver methods like get(), navigate.to(), navigate.refresh(), navigate.forward(), navigate.backward() waits automatically till the next page gets loaded completely.

In Selenium RC, we can acheive that using

new Selenium().waitForPagetoLoad()

Upvotes: 0

ddavison
ddavison

Reputation: 29092

If you are using Selenium RC, then it's the waitForPageToLoad() method.

selenium.waitForPagetoLoad() // it might be waitForLoad() - can't remember.

If you are using Selenium WebDriver, then all navigations are handled by the API and it will wait implicitly until the browser is done navigating.

driver.navigate().to().url("http://localhost:9000/web");

Also, make sure you are putting the "http://" at the beginning. Selenium might not be resolving "localhost:9000"

Upvotes: 0

Related Questions