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.
There are three ways to implement Selenium wait for page to load: Using Implicit Wait. Using Explicit Wait. Using Fluent Wait.
until(conditionToCheck); This way you can wait for your element to be present before executing your test2.
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.
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.
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);
}
}
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"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With