Can anyone let me how can I make selenium wait until the time the page loads completely? I want something generic, I know I can configure WebDriverWait and call something like 'find' to make it wait but I don't go that far. I just need to test that the page loads successfully and move on to next page to test.
I found something in .net but couldn't make it work in java ...
IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
Any thoughts anyone?
For the people who need to wait for a specific element to show up. (used c#)
Then if you want to wait for example if an class="error-message" exists in the DOM you simply do:
For id, it will then be
WaitForElement(driver, By.Id("yourid"));
Here's my attempt at a completely generic solution, in Python:
First, a generic "wait" function (use a WebDriverWait if you like, I find them ugly):
Next, the solution relies on the fact that selenium records an (internal) id-number for all elements on a page, including the top-level
<html>
element. When a page refreshes or loads, it gets a new html element with a new ID.So, assuming you want to click on a link with text "my link" for example:
For more Pythonic, reusable, generic helper, you can make a context manager:
And then you can use it on pretty much any selenium interaction:
I reckon that's bulletproof! What do you think?
More info in a blog post about it here
You can have the thread sleep till the page is reloaded. This is not the best solution, because you need to have an estimate of how long does the page take to load.
I Checked page load complete, work in Selenium 3.14.0
This is a working Java version of the example you gave :
Example For c#: