Wait until page is loaded with Selenium WebDriver for Python.

We can wait until the page is loaded with Selenium WebDriver using synchronization concepts. Selenium provides implicit and explicit wait mechanisms. To wait until the page is loaded, we use the explicit wait approach.

The explicit wait depends on expected conditions for particular element behaviors. For waiting until the page loads, we use expected conditions like presence_of_element_located for a specific element. If the wait time elapses without the condition being met, a timeout error is thrown.

Required Imports

To implement explicit wait conditions, we need the WebDriverWait and expected_conditions classes ?

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By

Basic Wait Until Page Loads

This example waits for an h1 element to be present, indicating the page has loaded ?

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")

# Wait for h1 element to be present (page loaded)
try:
    wait = WebDriverWait(driver, 10)
    wait.until(EC.presence_of_element_located((By.TAG_NAME, "h1")))
    print("Page loaded successfully")
except TimeoutException:
    print("Timeout: Page did not load within 10 seconds")

driver.quit()

Different Wait Conditions

Various expected conditions can be used to wait for page loading ?

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.tutorialspoint.com/index.htm")
wait = WebDriverWait(driver, 10)

# Method 1: Wait for specific element to be present
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "content")))

# Method 2: Wait for element to be visible
wait.until(EC.visibility_of_element_located((By.ID, "header")))

# Method 3: Wait for page title to contain specific text
wait.until(EC.title_contains("Tutorials"))

print("Page fully loaded and ready")
driver.quit()

Complete Example with Error Handling

A robust implementation with proper error handling and multiple wait conditions ?

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.exceptions import TimeoutException, WebDriverException
from selenium.webdriver.common.by import By
import time

def wait_for_page_load(driver, timeout=10):
    """Wait for page to load completely"""
    try:
        wait = WebDriverWait(driver, timeout)
        
        # Wait for document ready state
        wait.until(lambda driver: driver.execute_script("return document.readyState") == "complete")
        
        # Wait for specific element indicating page is loaded
        wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
        
        return True
    except TimeoutException:
        print(f"Page did not load within {timeout} seconds")
        return False

# Usage
driver = webdriver.Chrome()

try:
    driver.get("https://www.tutorialspoint.com/python/index.htm")
    
    if wait_for_page_load(driver, 15):
        print("Page loaded successfully")
        print(f"Page title: {driver.title}")
    else:
        print("Failed to load page")
        
except WebDriverException as e:
    print(f"WebDriver error: {e}")
finally:
    driver.quit()

Common Expected Conditions

Condition Description Use Case
presence_of_element_located Element is present in DOM Basic page load check
visibility_of_element_located Element is visible Wait for visible content
title_contains Page title contains text Verify correct page loaded
element_to_be_clickable Element is clickable Wait for interactive elements

Conclusion

Use WebDriverWait with expected_conditions to wait for page loading in Selenium. Choose appropriate conditions like presence_of_element_located for basic checks or visibility_of_element_located for interactive elements. Always include timeout handling to prevent indefinite waits.

---
Updated on: 2026-03-25T11:47:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements