2

The WebDriver documentation states the following for the driver.get() operation:

Dependent on several factors, including the OS/Browser combination, WebDriver may or may not wait for the page to load. In some circumstances, WebDriver may return control before the page has finished, or even started, loading

Can anyone explain under what circumstances WebDriver returns control before the page has finished, or even started, loading?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
badhaircut
  • 355
  • 4
  • 8

2 Answers2

1

In a simple form, .get() navigates you to a URL.

Can anyone explain under what circumstances WebDriver returns control before the page has finished, or even started, loading?

This kind of situation is pretty common nowadays - there are more and more pages out there that load asynchronously. Selenium would not wait for responses to outstanding AJAX requests or currently running scripts which can cause different errors while trying to access elements that are not present in DOM, or are not yet visible, or not yet having the desired data.

And this is, by the way, one of the reasons for the protractor package to exist specifically for AngularJS application testing - it listens for an angular to signal "okay, I'm done with this page".

There are multiple ways to wait for a page to load:

Here is a perfect answer that should help in clearing things up.

Also see related topics with some great explanations of the problem:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

It depends on the WebDriver you are using.

In almost every WebDriver, control is given after document loaded. This means there is a possibility a onDocumentReady JavaScript callback is not yet finished.

To circumvent this, you can use a WebDriverWait or a FluentWait with a suitable predicate.

A Thread.sleep() is also possible, but a rather dirty way!

Stefaan Neyts
  • 2,054
  • 1
  • 16
  • 25
  • When you say "control is given after document loaded", how does WebDriver define "document loaded"? – badhaircut Mar 17 '15 at 20:05
  • Depends on the driver. You can have a look at GrepCode for this. Here is an example of HtmlUnitDriver: http://grepcode.com/search/usages?type=method&id=repo1.maven.org%24maven2@net.sourceforge.htmlunit%24htmlunit@2.15@com%24gargoylesoftware%24htmlunit@WebConnection@getResponse%28com.gargoylesoftware.htmlunit.WebRequest%29&k=uttp://grepcode.com/search/usages?type=method&id=repo1.maven.org%24maven2@net.sourceforge.htmlunit%24htmlunit@2.15@com%24gargoylesoftware%24htmlunit@WebConnection@getResponse%28com.gargoylesoftware.htmlunit.WebRequest%29&k=u – Stefaan Neyts Mar 17 '15 at 20:18