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....
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:
- Do we have any generic function to check if page has completely loaded in Selenium
- Selenium IE WebDriver only works while debugging
- Selenium how to manage wait for page load?