How to Use Explicit Wait in Selenium Python for Reliable Tests
Use
WebDriverWait with expected_conditions in Selenium Python to wait explicitly for an element or condition before proceeding. This helps avoid errors by pausing test execution until the element is ready or a timeout occurs.Syntax
The explicit wait in Selenium Python uses WebDriverWait combined with expected_conditions to wait for a specific condition on an element.
Key parts:
WebDriverWait(driver, timeout): creates a wait object with a max wait time in seconds.until(condition): waits until the condition is true or timeout happens.expected_conditions: predefined conditions like element visibility or clickability.
python
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By wait = WebDriverWait(driver, 10) # wait up to 10 seconds element = wait.until(EC.visibility_of_element_located((By.ID, 'element_id')))
Example
This example opens a webpage, waits explicitly for a button with ID submit-btn to be clickable, then clicks it. It shows how explicit wait avoids errors if the button loads slowly.
python
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 # Setup WebDriver (Chrome in this example) driver = webdriver.Chrome() driver.get('https://example.com') try: wait = WebDriverWait(driver, 10) # wait max 10 seconds button = wait.until(EC.element_to_be_clickable((By.ID, 'submit-btn'))) button.click() print('Button clicked successfully') except Exception as e: print('Error:', e) finally: driver.quit()
Output
Button clicked successfully
Common Pitfalls
Common mistakes when using explicit wait include:
- Using implicit wait and explicit wait together, which can cause unpredictable delays.
- Waiting for wrong conditions, like
presence_of_element_locatedwhen you needvisibility_of_element_located. - Not using a tuple for locator, e.g., missing extra parentheses around
(By.ID, 'id'). - Setting too short or too long timeout values.
python
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # Wrong: missing tuple parentheses # wait.until(EC.visibility_of_element_located(By.ID, 'submit-btn')) # This will raise an error # Right: wait.until(EC.visibility_of_element_located((By.ID, 'submit-btn')))
Quick Reference
| Method | Description |
|---|---|
| WebDriverWait(driver, timeout) | Creates an explicit wait object with max wait time in seconds |
| until(condition) | Waits until the given condition is true or timeout occurs |
| expected_conditions.visibility_of_element_located(locator) | Waits until element is visible on page |
| expected_conditions.element_to_be_clickable(locator) | Waits until element is clickable |
| expected_conditions.presence_of_element_located(locator) | Waits until element is present in DOM (may be hidden) |
Key Takeaways
Use WebDriverWait with expected_conditions for reliable explicit waits in Selenium Python.
Always pass locators as tuples, e.g., (By.ID, 'id'), to expected_conditions.
Avoid mixing implicit and explicit waits to prevent unexpected delays.
Choose the correct expected condition based on what you need to wait for (visibility, clickability, presence).
Set reasonable timeout values to balance test speed and reliability.