How to Use expected_conditions in Selenium for Reliable Tests
Use
expected_conditions in Selenium with WebDriverWait to wait for specific conditions like element visibility or clickability before interacting with elements. Import expected_conditions as EC and pass the condition to WebDriverWait(driver, timeout).until() to pause test execution until the condition is met or timeout occurs.Syntax
The basic syntax to use expected_conditions involves importing it as EC, then using WebDriverWait with a driver and timeout. You call until() with an EC condition to wait for that condition.
WebDriverWait(driver, timeout): creates a wait object with a max wait time.until(condition): waits until the condition is true or timeout.EC.condition(locator): predefined conditions like visibility, presence, or clickability of elements.
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 wait.until(EC.visibility_of_element_located((By.ID, 'element_id')))
Example
This example shows how to wait for a button to become clickable before clicking it. It demonstrates using expected_conditions with WebDriverWait to avoid errors from trying to click too early.
python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Setup WebDriver (Chrome in this case) driver = webdriver.Chrome() driver.get('https://example.com') try: # Wait up to 10 seconds for the button to be clickable wait = WebDriverWait(driver, 10) button = wait.until(EC.element_to_be_clickable((By.ID, 'submit-button'))) button.click() print('Button clicked successfully') except Exception as e: print(f'Error: {e}') finally: driver.quit()
Output
Button clicked successfully
Common Pitfalls
Common mistakes when using expected_conditions include:
- Not importing
expected_conditionsasEC, causing errors. - Passing locator tuples incorrectly (must be inside another tuple).
- Using
find_elementbefore waiting, which can causeNoSuchElementException. - Not handling timeout exceptions, leading to test crashes.
Always use WebDriverWait with EC conditions and handle exceptions gracefully.
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: locator tuple missing extra parentheses wait.until(EC.visibility_of_element_located(By.ID, 'element_id')) # This will raise an error # Right: locator tuple inside another tuple wait.until(EC.visibility_of_element_located((By.ID, 'element_id')))
Quick Reference
| Condition | Description | Usage Example |
|---|---|---|
| presence_of_element_located | Waits until element is present in DOM | EC.presence_of_element_located((By.ID, 'id')) |
| visibility_of_element_located | Waits until element is visible | EC.visibility_of_element_located((By.CSS_SELECTOR, '.class')) |
| element_to_be_clickable | Waits until element is clickable | EC.element_to_be_clickable((By.NAME, 'name')) |
| text_to_be_present_in_element | Waits until text appears in element | EC.text_to_be_present_in_element((By.TAG_NAME, 'p'), 'Hello') |
| alert_is_present | Waits until alert is present | EC.alert_is_present() |
Key Takeaways
Use WebDriverWait with expected_conditions to wait for elements or states before actions.
Always pass locator tuples inside another tuple, e.g., (By.ID, 'id').
Handle timeout exceptions to avoid test crashes.
Common conditions include visibility, clickability, presence, and alerts.
Import expected_conditions as EC for cleaner, readable code.