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

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)

References

You can find a couple of relevant discussions in:

Leave a Comment