Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium driver - wait on page reload

Tags:

selenium

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.

like image 791
ryskajakub Avatar asked Oct 23 '13 21:10

ryskajakub


People also ask

Does Selenium wait for page to load?

There are three ways to implement Selenium wait for page to load: Using Implicit Wait. Using Explicit Wait. Using Fluent Wait.

What would you do to make the WebDriver wait for a page to refresh before executing the test?

until(conditionToCheck); This way you can wait for your element to be present before executing your test2.

Which wait statement will be used to wait till page load?

Explicit Wait The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an "ElementNotVisibleException" exception.

How do I wait for drivers in Selenium?

To use Explicit Wait in test scripts, import the following packages into the script. Then, Initialize A Wait Object using WebDriverWait Class. Here, the reference variable is named <wait> for the <WebDriverWait> class. It is instantiated using the WebDriver instance.


2 Answers

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);
    }
}
like image 63
Sitam Jana Avatar answered Oct 14 '22 06:10

Sitam Jana


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"));
}
like image 43
rrw Avatar answered Oct 14 '22 08:10

rrw