Wait until page is loaded with Selenium WebDriver for Python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Selenium, "wait until the page is loaded" usually should not mean sleeping for an arbitrary number of seconds. Modern pages often finish navigation before the content you care about is ready, or they keep updating long after the initial load event. The practical solution is to wait for a specific condition that proves the page is usable for your next step.
Prefer Explicit Waits
The best default tool is WebDriverWait with an expected condition.
This is better than time.sleep because it waits only as long as needed and ties the wait to a meaningful condition.
document.readyState Is Not Always Enough
A common trick is to wait for the browser document state to become complete.
This can be useful for traditional full-page loads, but it is not a universal answer.
Why not:
- single-page apps can keep loading data after
readyStatebecomescomplete - AJAX content may appear later
- the element you need may still be missing or not interactable
So readyState is at best a low-level navigation signal, not proof that the workflow can continue.
Wait for the Thing You Actually Need
If the next action is clicking a button, wait for that button to be clickable. If the next action is reading a table, wait for the table rows to appear.
This kind of wait is more robust because it is tied to user-level intent.
Combining Conditions
Some workflows benefit from waiting for navigation readiness and then waiting for a page-specific element.
This is often a good balance when you want both a completed navigation and a visible application feature.
Avoid Overusing Implicit Waits
Implicit waits apply globally and can make debugging harder because every element lookup silently waits.
This is not always wrong, but explicit waits are usually clearer because they make the waited-for condition visible at the exact place it matters.
For most serious test suites and automation scripts, explicit waits are the better default.
A Reusable Helper
If you need the same page-ready check in several places, wrap it in a helper.
Then call a page-specific wait afterwards if needed.
Common Pitfalls
- Using
time.sleepas the primary waiting strategy. - Treating
document.readyState == "complete"as proof that a dynamic application is ready for interaction. - Waiting for the page in general when the real dependency is a specific element or state.
- Mixing heavy implicit waits with explicit waits and then wondering why timing behavior is inconsistent.
- Catching timeout exceptions without logging which condition actually failed.
Summary
- In Selenium, the best wait is usually a wait for a specific meaningful condition, not a fixed sleep.
- Use
WebDriverWaitwith expected conditions for visibility, presence, or clickability. - '
document.readyStatecan help, but it is not enough for many dynamic pages.' - Wait for the element or application state your next action actually depends on.
- Explicit waits are usually clearer and more reliable than broad implicit waits.

