I have a web application to test with Selenium. There is a lot of JavaScript running on page load.
This JavaScript code is not so well written but I can't change anything. So waiting for an element to appear in the DOM with findElement()
method is not an option.
I want to create a generic function in Java to wait for a page to load, a possible solution would be:
document.body.innerHTML
in a string variable body
.body
variable to the previous version of body
. if they are the same then set increment a counter notChangedCount
otherwise set notChangedCount
to zero.notChangedCount >= 10
then exit the loop otherwise loop to the first step.Do you think it's a valid solution?
We can run Javascript in Selenium webdriver with Python. The Document Object Model communicates with the elements on the page with the help of Javascript. Selenium executes the Javascript commands by taking the help of the execute_script method. The commands to be executed are passed as arguments to the method.
We can wait until the page is completely loaded in Selenium webdriver by using the JavaScript Executor. Selenium can run JavaScript commands with the help of the executeScript method. We have to pass return document.
If anyone actually knew a general and always-applicable answer, it would have been implemented everywhere ages ago and would make our lives SO much easier.
There are many things you can do, but every single one of them has a problem:
As Ashwin Prabhu said, if you know the script well, you can observe its behaviour and track some of its variables on window
or document
etc. This solution, however, is not for everyone and can be used only by you and only on a limited set of pages.
Your solution by observing the HTML code and whether it has or hasn't been changed for some time is not bad (also, there is a method to get the original and not-edited HTML directly by WebDriver
), but:
setTimeout()
) and work again and again and could possibly change the HTML every time they run. Seriously, every "Web 2.0" page does it. Even Stack Overflow. You could overwrite the most common methods used and consider the scripts that use them as completed, but ... you can't be sure.innerHTML
fun.There are tools to help you on this. Namely Progress Listeners together with nsIWebProgressListener and some others. The browser support for this, however, is horrible. Firefox began to try to support it from FF4 onwards (still evolving), IE has basic support in IE9.
And I guess I could come up with another flawed solution soon. The fact is - there's no definite answer on when to say "now the page is complete" because of the everlasting scripts doing their work. Pick the one that serves you best, but beware of its shortcomings.
Thanks Ashwin !
In my case I should need wait for a jquery plugin execution in some element.. specifically "qtip"
based in your hint, it worked perfectly for me :
wait.until( new Predicate<WebDriver>() { public boolean apply(WebDriver driver) { return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"); } } );
Note: I'm using Webdriver 2
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