Explicit waits help your test wait for a specific condition before moving on. This avoids errors when elements take time to appear or change.
Explicit waits (WebDriverWait) 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((By.LOCATOR_TYPE, 'locator_value')))
WebDriverWait creates a wait object with a max time to wait.
until() pauses test until the condition is true or timeout happens.
wait = WebDriverWait(driver, 10) wait.until(EC.element_to_be_clickable((By.ID, 'submit-btn')))
wait = WebDriverWait(driver, 5) wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.message')))
This script opens a webpage and waits up to 10 seconds for a login button to be clickable. It prints a success message if clickable, or an error if not.
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 (using Chrome as example) driver = webdriver.Chrome() driver.get('https://example.com') try: wait = WebDriverWait(driver, 10) # Wait for the login button to be clickable login_button = wait.until(EC.element_to_be_clickable((By.ID, 'login-btn'))) print('Login button is clickable.') except Exception as e: print('Login button not clickable:', e) finally: driver.quit()
Always import expected_conditions as EC for readability.
Use tuples like (By.ID, 'value') as locators inside conditions.
Explicit waits are better than fixed sleeps because they wait only as long as needed.
Explicit waits pause test until a condition is met or timeout occurs.
Use WebDriverWait with expected conditions for reliable element interaction.
This helps tests handle slow loading or dynamic page changes smoothly.
Practice
WebDriverWait in Selenium tests?Solution
Step 1: Understand the role of explicit waits
Explicit waits pause the test until a specific condition is true or a timeout happens, making tests more reliable.Step 2: Compare with other wait types
Unlike fixed waits, explicit waits respond to page conditions, not just time delays.Final Answer:
To wait explicitly until a certain condition is met or timeout occurs -> Option DQuick Check:
Explicit wait = wait for condition [OK]
- Confusing explicit wait with fixed sleep
- Thinking it speeds up tests
- Assuming it retries tests automatically
Solution
Step 1: Recall WebDriverWait constructor parameters
The constructor takes the driver first, then the timeout as an integer or float in seconds.Step 2: Check each option's correctness
wait = WebDriverWait(driver, 10) matches the correct order and type. wait = WebDriverWait(10, driver) reverses parameters. wait = WebDriverWait(driver, timeout=10s) uses invalid syntax with '10s'. wait = WebDriverWait(driver, "10") passes timeout as string, which is incorrect.Final Answer:
wait = WebDriverWait(driver, 10) -> Option AQuick Check:
WebDriverWait(driver, seconds) [OK]
- Swapping driver and timeout parameters
- Using string instead of number for timeout
- Adding units like 's' in timeout
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, 5)
element = wait.until(EC.element_to_be_clickable((By.ID, 'submit')))
print('Element found:', element is not None)Solution
Step 1: Understand wait.until behavior
The wait will pause up to 5 seconds for the element to be clickable. Since the element appears after 3 seconds, it will be found within timeout.Step 2: Check the print statement output
The element variable will hold the found element, so 'element is not None' is True, printing 'Element found: True'.Final Answer:
Element found: True -> Option BQuick Check:
Element appears before timeout = True [OK]
- Assuming wait.until returns False if element appears
- Expecting TimeoutException when element appears in time
- Confusing syntax errors with runtime behavior
wait = WebDriverWait(driver, 10) wait.until(EC.presence_of_element_located(By.ID, 'username'))
Solution
Step 1: Check expected condition usage
The method presence_of_element_located expects a single argument: a tuple like (By.ID, 'username').Step 2: Identify the mistake in argument format
The code passes By.ID and 'username' as separate arguments, missing the tuple, causing a TypeError.Final Answer:
Missing tuple around locator in presence_of_element_located -> Option AQuick Check:
Locator must be tuple (By.ID, 'value') [OK]
- Passing locator arguments separately instead of tuple
- Using string timeout instead of number
- Thinking polling parameter is mandatory
Solution
Step 1: Understand waiting for multiple conditions
WebDriverWait can accept a custom function that returns True when either condition is met, allowing waiting for multiple conditions simultaneously.Step 2: Evaluate options for efficiency and correctness
Use WebDriverWait with a custom expected condition combining both checks using a lambda function uses a lambda combining both conditions, so wait ends as soon as one is true. Use two separate WebDriverWait calls one after another for each condition waits sequentially, possibly longer. Use implicit wait instead of explicit wait for both elements uses implicit wait, which is not suitable for condition checks. Use time.sleep() to wait a fixed time before checking both elements uses fixed sleep, which is unreliable.Final Answer:
Use WebDriverWait with a custom expected condition combining both checks using a lambda function -> Option CQuick Check:
Custom combined condition = fastest wait [OK]
- Waiting sequentially causing longer delays
- Using implicit wait for condition checks
- Using fixed sleep instead of waits
Ready to prove it?
Pass the test and this pattern moves unknown
