0
public void waitForPageLoad(WebDriver driver){
    while(true){
        String page_status =(String)((JavascriptExecutor)driver).executeScript("return document.readyState");
        if(page_status.equals("complete")){
            break;
        }
    }
}  

I saw people using the above method in framework to wait till page finishes the JS loading.
But why to write this method as the selenium scripts doesn't execute when we navigate to next page and the page isn't loaded completely.
I guess this is used if any action made is refreshing the page, say removing a product from the cart of a online shopping site ?
Am i correct or is there any other reason ?

1
  • this is required, as while navigating from one page to other it wont wait for all elements to be loaded and start executing the selenium script which can result in element not found exceptions. with webdriver get method, it waits for page to load completely by default. but not in case of navigation.
    – The_Amol
    Commented Feb 24, 2016 at 11:04

1 Answer 1

1

But why to write this method as the selenium scripts doesn't execute when we navigate to next page and the page isn't loaded completely.

Ok I totally get why you feel that piece of code is trivial. Well we all know how good a tool Selenium is and we expect it should be at least wait for page load out. Unfortunately it is not that simple for Selenium, as it doesn't know if you clicked a real link which takes to new URL or link to the same page or some JS to do some rich UI stuff.

There is no way for Selenium to track the impact that a click has on the browsers' internals. It might try to poll the browser for its page-loaded status immediately after clicking, but that's open to a race condition where the browser was multitasking, hasn't quite got round to dealing with the click yet.

Hope that clarifies your query.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.