seleniumhq.github.io icon indicating copy to clipboard operation
seleniumhq.github.io copied to clipboard

Was confused after reading: https://www.selenium.dev/documentation/en/webdriver/page_loading_strategy/

Open diemol opened this issue 4 years ago • 4 comments

Was confused after reading: https://www.selenium.dev/documentation/en/webdriver/page_loading_strategy/

"This will make Selenium WebDriver to wait until the initial HTML document has been completely loaded and parsed, and discards loading of stylesheets, images and subframes."

Are you saying that they are loaded, just not processed?

Originally posted by @githubuser100007 in https://github.com/SeleniumHQ/selenium/issues/9315#issuecomment-808208834

diemol avatar Mar 26 '21 13:03 diemol

The following are based on my understanding after reading the documentations. Hope that they are clear enough for you.

Default Behaviour - normal

The default behaviour of Selenium is to wait until the whole HTML document and stylesheets and images and more to complete their loading. To be more specific, it is when the load event is fired.

Quoting from Mozilla's documentation of load event:

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images.

Once all contents are loaded, Selenium continues the automation as programmed.

User may want to use a different Page Load Strategy

However, downloading and loading the other resources may take longer time than the those of HTML document. There may also be cases where the HTML document is sufficient for the Selenium automation, because obviously, Selenium does not read the webpage the way as any average human does, and does not care about styles and images unless requested.

Thus, the main use case of setting a different page load strategy is when the user wants to speed up the process and decides not to wait until everything is completely downloaded and loaded.

Alternative 1 - Eager

When Page Load Strategy is set to eager, Selenium waits until DOMContentLoaded event is fired.

Quoting from Mozilla's documentation of DOMContentLoaded event:

The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

That means, Selenium continues running after the HTML document is loaded while ignoring whether the other resources are still being downloaded and/or loaded.

Imagine that the user is automating for a webpage displaying 50 high resolution images each of, say, 20 MB, and these add up to nearly 1 GB. It may be sensible to start interacting with the website before all images are loaded, which can take a long long long time for most computers and networks as of writing this (year 2022).

Alternative 2 - None

When Page Load Strategy is set to none, Selenium takes it to the extreme that it only waits until the initial HTML document is downloaded (no guarantee that it is loaded). We don't even care about the other resources!

Obviously, this strategy is not frequently used and I cannot think of a use case for this but at least the option is here.

Remarks

  • Since this is a browser capability that affects the whole session, users should think carefully when switching to eager or none.
  • Page load strategy does not take contents that are loaded using JavaScript into consideration.
  • Needless to say, adding webdriver (explicit/implicit) waits is a good idea. And a good combination of page load strategy and waits may speed up the automation considerably.

nicolas-chan-42 avatar Feb 20 '22 19:02 nicolas-chan-42

Thank you for your comment, @nicolas-chan-42!

Would you like to create a PR with a summarized version of this content for this page? https://www.selenium.dev/documentation/webdriver/capabilities/shared/#pageloadstrategy

diemol avatar Feb 21 '22 10:02 diemol

Can I pick this up ?

jags14385 avatar Jul 30 '22 20:07 jags14385

Sure, @jags14385, go for it!

diemol avatar Aug 01 '22 08:08 diemol

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Sep 19 '22 02:09 github-actions[bot]