Selenium
WebDriver
Python
Page Load
Automation

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.

python
1from selenium import webdriver
2from selenium.webdriver.common.by import By
3from selenium.webdriver.support.ui import WebDriverWait
4from selenium.webdriver.support import expected_conditions as EC
5
6browser = webdriver.Chrome()
7browser.get("https://example.com")
8
9wait = WebDriverWait(browser, 10)
10element = wait.until(EC.visibility_of_element_located((By.ID, "main")))
11print(element.text)

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.

python
1from selenium.webdriver.support.ui import WebDriverWait
2
3WebDriverWait(browser, 10).until(
4    lambda driver: driver.execute_script("return document.readyState") == "complete"
5)

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 readyState becomes complete
  • 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.

python
1from selenium.webdriver.common.by import By
2from selenium.webdriver.support.ui import WebDriverWait
3from selenium.webdriver.support import expected_conditions as EC
4
5wait = WebDriverWait(browser, 10)
6button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".save-button")))
7button.click()

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.

python
1from selenium.webdriver.support.ui import WebDriverWait
2from selenium.webdriver.common.by import By
3from selenium.webdriver.support import expected_conditions as EC
4
5wait = WebDriverWait(browser, 10)
6wait.until(lambda driver: driver.execute_script("return document.readyState") == "complete")
7wait.until(EC.presence_of_element_located((By.ID, "results")))

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.

python
browser.implicitly_wait(10)

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.

python
1from selenium.webdriver.support.ui import WebDriverWait
2
3
4def wait_for_page_ready(driver, timeout=10):
5    WebDriverWait(driver, timeout).until(
6        lambda d: d.execute_script("return document.readyState") == "complete"
7    )

Then call a page-specific wait afterwards if needed.

Common Pitfalls

  • Using time.sleep as 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 WebDriverWait with expected conditions for visibility, presence, or clickability.
  • 'document.readyState can 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.

Course illustration
Course illustration

All Rights Reserved.