0
0
Selenium-pythonHow-ToBeginner · 4 min read

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_conditions as EC, causing errors.
  • Passing locator tuples incorrectly (must be inside another tuple).
  • Using find_element before waiting, which can cause NoSuchElementException.
  • 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

ConditionDescriptionUsage Example
presence_of_element_locatedWaits until element is present in DOMEC.presence_of_element_located((By.ID, 'id'))
visibility_of_element_locatedWaits until element is visibleEC.visibility_of_element_located((By.CSS_SELECTOR, '.class'))
element_to_be_clickableWaits until element is clickableEC.element_to_be_clickable((By.NAME, 'name'))
text_to_be_present_in_elementWaits until text appears in elementEC.text_to_be_present_in_element((By.TAG_NAME, 'p'), 'Hello')
alert_is_presentWaits until alert is presentEC.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.