Expected conditions help your test wait for things on a web page to be ready before checking them. This stops errors caused by trying to use parts of the page too early.
Expected conditions in Selenium Python
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, timeout_in_seconds) wait.until(EC.condition(locator))
WebDriverWait waits for a maximum time for a condition to be true.
EC.condition(locator) is the expected condition you want to check, like visibility or clickability.
wait.until(EC.visibility_of_element_located((By.ID, 'submit-button')))wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-primary')))wait.until(EC.url_contains('dashboard'))This script opens example.com, waits up to 10 seconds for the main heading (h1) to appear, then prints its text. It closes the browser after.
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 driver (make sure chromedriver is in PATH) driver = webdriver.Chrome() try: driver.get('https://example.com') wait = WebDriverWait(driver, 10) # Wait for the heading to be visible heading = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'h1'))) print('Heading text:', heading.text) finally: driver.quit()
Always use tuples like (By.ID, 'value') as locators inside expected conditions.
Set a reasonable timeout to avoid waiting too long or too short.
Expected conditions help make tests more reliable by syncing with the page state.
Expected conditions wait for specific page states before continuing.
Use WebDriverWait with EC to avoid errors from elements not ready yet.
Common conditions include visibility, clickability, and URL changes.
Practice
ExpectedConditions in Selenium WebDriver?Solution
Step 1: Understand ExpectedConditions role
ExpectedConditions help Selenium wait for specific states like element visibility or clickability before proceeding.Step 2: Identify the main purpose
This waiting avoids errors caused by trying to interact with elements not yet ready on the page.Final Answer:
To wait for certain conditions before interacting with elements -> Option AQuick Check:
ExpectedConditions = Wait for conditions [OK]
- Thinking ExpectedConditions speed up tests by skipping waits
- Confusing ExpectedConditions with data generation
- Assuming ExpectedConditions take screenshots
Solution
Step 1: Check correct ExpectedCondition usage
element_to_be_clickable requires a locator tuple like (By.ID, 'submit').Step 2: Verify syntax correctness
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'submit'))) correctly uses a tuple inside the condition and waits properly.Final Answer:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'submit'))) -> Option BQuick Check:
Use tuple locator with element_to_be_clickable [OK]
- Passing string instead of locator tuple
- Using visibility_of_element_located instead of clickable
- Missing parentheses around locator tuple
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) element = wait.until(EC.visibility_of_element_located((By.ID, 'login'))) print(element.is_displayed())
Solution
Step 1: Analyze wait duration and element visibility
The wait is 10 seconds, and the element appears after 5 seconds, so wait.until succeeds.Step 2: Understand is_displayed() output
Since the element is visible, element.is_displayed() returns True, which is printed.Final Answer:
True -> Option CQuick Check:
Element visible within wait = True output [OK]
- Expecting TimeoutException though element appears in time
- Confusing is_displayed() with presence only
- Assuming False if element appears late but within wait
wait = WebDriverWait(driver, 5) wait.until(EC.element_to_be_clickable(By.CLASS_NAME, 'btn'))
Solution
Step 1: Check element_to_be_clickable parameter
It requires a single locator tuple like (By.CLASS_NAME, 'btn'), not two separate arguments.Step 2: Identify syntax error
Code passes two arguments instead of one tuple, causing a TypeError.Final Answer:
Missing tuple around locator in element_to_be_clickable -> Option DQuick Check:
Locator must be a tuple inside ExpectedConditions [OK]
- Passing locator arguments separately instead of tuple
- Assuming timeout value causes error here
- Confusing locator types (CLASS_NAME vs CSS_SELECTOR)
Solution
Step 1: Identify correct ExpectedCondition for URL substring
EC.url_contains waits until the URL contains the given substring.Step 2: Verify other options
url_matches requires a regex pattern to match the entire URL, url_to_be checks for exact URL equality, and url_is is not a valid ExpectedCondition.Final Answer:
WebDriverWait(driver, 15).until(EC.url_contains('dashboard')) -> Option AQuick Check:
Use url_contains for substring in URL [OK]
- Using non-existent ExpectedConditions like url_is
- Confusing url_contains with exact match conditions like url_to_be
- Using url_matches without proper regex for substring
Ready to prove it?
Pass the test and this pattern moves unknown
