Re: [webdriver] How to wait until total page load and how to prevent total page load if required elements already loaded???

346 views
Skip to first unread message

Fakrudeen Shahul

unread,
Apr 10, 2013, 3:21:40 AM4/10/13
to webd...@googlegroups.com
Mostly images in a page takes long time to load. So waiting for the image to load is one way to ensure that the page is loaded completely
.

With Regards,
Fakrudeen


On Tue, Apr 9, 2013 at 5:56 PM, <reply...@gmail.com> wrote:
Hi All,

  Here I am giving two topics.

1: How to wait until my entire page loads.Here dont know the exact element number in the page. page loads very slowly might be system issue, or connection issues(slow net).Load time whatever it might be need to wait page load fully. How can achieve this.

2:Page is loading very slowly but required elements already present(Eg: Username, pwd, signin) so need to move to another page.What are the ways we can achieve this.

Thanks,
Subhash

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

darrell

unread,
Apr 10, 2013, 10:32:01 PM4/10/13
to webd...@googlegroups.com
There is the onload event and there is the concept of a page fully loaded. I believe Selenium will wait for the onload event. Once the onload event is fired, the Ajax and Javascript starts running. This code could then load more things into the DOM. For example a page might be 200 bytes and only be an empty <body> and lots of Javascript. The moment the 200 bytes are loaded an onload event will fire. The Javascript might then start loading megabytes of other data and populate the DOM with <DIV>, <SPAN>, <TABLE>, etc.

So the question is, do you want to wait for the onload event to be fired? Or do you want to wait for all network activity and Javascript activity to finish? The onload event should be easy. It will just happen without you doing anything special. If you want to wait for all the elements to be loaded (via Ajax) and the DOM to be built (via Javascript) then it becomes a lot harder.

On some projects I have had the developers expose Ajax code which tells me when it is done. I can then use a JavascriptExecutor to check if the page is fully loaded. The developers would increment a counter for each Ajax call. When the Ajax call returned they would decrement the counter. When the counter when to zero it meant that all the Ajax calls had returned and the page was fully loaded. I needed the developers to do something for this to work.

Some Javascript libraries will give you a counter for free. If the team is always using the library call, you can leverage the counter which comes with the library.

The other option is to just look for specific WebElement to load. When they have all loaded you can count the page as fully loaded. Or you could try:

    driver.findElements(By.xpath("//*"));

to get all the elements loaded. You could loop until the .size() of the List reaches the expected amount (not a good idea) or you could get the List, save current size, wait 1 second, get the List and see if it current size > previous size. If the size is still growing, keep looping. If not, assume the page is loaded and finish the method, e.g.

public boolean waitForPageFullyLoaded(WebDriver driver, int timeoutMs) {
    int previous;
    int current = 0;
    int timeSliceMs = 1000;
    do {
        previous = current;
        Thread.sleep(timeSliceMs);
        timeoutMs -= timeSliceMs;
        current = driver.findElements(By.xpath("//*")).size();
    } while(current > previous && timeoutMs > 0);
    if(timeoutMs > 0) {
        return true;
    }
    return false;
}

Darrell

reply...@gmail.com

unread,
Apr 16, 2013, 3:49:23 AM4/16/13
to webd...@googlegroups.com
Hi All,

  Great thanks all for yr valuable information.Really helpful

Regards,
Subhash
Reply all
Reply to author
Forward
0 new messages