Is there a way with python-selenium to wait until all elements of a page has loaded?

Posted on

Question :

Is there a way with python-selenium to wait until all elements of a page has loaded?

I am asking for generally checking if all elements of a page has been loaded. Is there a way to check that basically?

In the concrete example there is a page, I click on some button, and then I have to wait until I click on the ‘next’ button. However, this ‘Next’ button is available, selectable and clickable ALL THE TIME. So how to check with selenium that ‘state'(?) of a page?

As a reminder: This is a question about selenium and not the quality of the webpage in question….

Asked By: Alex


Answer #1:

As your question is about if there is a way with python-selenium to wait until all elements of a page has loaded, the Answer is No.

An Alternate Approach

Fundamentally, you can write a line of code (or implement it through a function) to check for the ‘document.readyState’ == “complete” as follows :

self.driver.execute_script("return document.readyState").equals("complete"))

But this approach have a drawback that it doesn’t accounts for the JavaScript / AJAX Calls to be completed.

Why No

Writing the above mentioned line to wait for Page Loading is implemented by default through Selenium. So rewriting the same is a complete overhead. The client (i.e. the Web Browser) will never return the control back to the WebDriver instance until and unless 'document.readyState' is equal to "complete". Once this condition is fulfilled Selenium performs the next line of code.

It’s worth to mention that though the client (i.e. the Web Browser) can return back the control to the WebDriver instance once 'document.readyState' equal to "complete" is achieved, it doesn’t guarantees whether all the WebElements on the new HTML DOM are present, visible, interactable and clickable.

So, as per our requirement, if the next *WebElement with which you have to interact is not interactable by the time 'document.readyState' equal to "complete" is achieved, you have to induce WebDriverWait inconjunction with a matching expected_conditions with respect to the individual WebElement. Here are a few of the most used expected_condition:

  • element_to_be_clickable(locator)
  • presence_of_element_located(locator)
  • visibility_of(element)


You can find a couple of relevant discussions in:

Answered By: DebanjanB

Answer #2:

Reliably determining whether a page has been fully loaded can be challenging. There is no way to know if all the elements have been loaded just like that. You must define some “anchor” points in each page so that as far as you aware, if these elements has been loaded, it is fair to assume the whole page has been loaded. Usually this involves a combination of tests. So for example, you can define that if the below combination of tests passes, the page is considered loaded:

  • JavaScript document.readyState === 'complete'
  • “Anchor” elements
  • All kinds of “spinners”, if exist, disappeared.
Answered By: Eugene S

Answer #3:

There is something called the document.readyState which you can retrieve by executing a JavaScript script via Selenium. This doesn’t work for dynamically loaded content. It returns one of these three states:

The document is still being loaded, no css or other resources are available

The document has been loaded, no css or other resources are available

Both the document and the css / other resources are loaded

You’re looking for at least Interactive. You can retrieve the state by calling execute_script:

driver.execute_script("return document.readyState")
Answered By: Marcel

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *