Bird
Raised Fist0
Selenium Pythontesting~20 mins

Explicit waits (WebDriverWait) in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Explicit Waits Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium explicit wait code?
Consider the following Selenium Python code snippet using explicit wait. What will be printed?
Selenium 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

class DummyElement:
    def __init__(self):
        self.text = "Loaded"

# Mock driver and wait for demonstration
class MockDriver:
    def find_element(self, by, value):
        return DummyElement()


driver = MockDriver()

try:
    element = WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.ID, "my-element"))
    )
    print(element.text)
except Exception:
    print("Timeout")
AAttributeError
BTimeout
CLoaded
DNoSuchElementException
Attempts:
2 left
💡 Hint
Think about what the mock driver returns and how WebDriverWait uses it.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies element visibility after explicit wait?
You want to assert that an element with ID 'submit-btn' is visible after waiting explicitly. Which assertion is correct?
Selenium Python
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, 'submit-btn')))
Aassert element is None
Bassert element.is_displayed() is False
Cassert element.text == 'submit-btn'
Dassert element.is_displayed() == True
Attempts:
2 left
💡 Hint
Visibility means the element is displayed on the page.
❓ locator
advanced
2:00remaining
Which locator is best for waiting for a button with text 'Submit' to be clickable?
You want to wait explicitly until a button with visible text 'Submit' is clickable. Which locator and condition combo is best?
ABy.XPATH, "//button[text()='Submit']" with EC.element_to_be_clickable
BBy.ID, "submit" with EC.presence_of_element_located
CBy.CSS_SELECTOR, "button:contains('Submit')" with EC.element_to_be_clickable
DBy.CLASS_NAME, "submit-btn" with EC.visibility_of_element_located
Attempts:
2 left
💡 Hint
Consider which locator matches exact button text and which condition ensures clickability.
🔧 Debug
advanced
2:00remaining
Why does this explicit wait code raise TimeoutException?
This code raises TimeoutException even though the element is present. Why?
Selenium Python
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)
wait.until(EC.visibility_of_element_located((By.ID, 'loading-spinner')))
AThe locator By.ID is invalid and causes the wait to fail
BThe element is present but not visible, so visibility_of_element_located times out
CThe wait timeout is too long causing an exception
DThe driver is not initialized before the wait
Attempts:
2 left
💡 Hint
Presence and visibility are different conditions.
❓ framework
expert
3:00remaining
How to implement a custom explicit wait condition for element attribute value?
You want to wait until an element with ID 'status' has attribute 'data-state' equal to 'ready'. Which custom expected condition implementation is correct?
A
class attribute_value_is:
    def __init__(self, locator, attribute, value):
        self.locator = locator
        self.attribute = attribute
        self.value = value
    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        return element.get_attribute(self.attribute) == self.value
B
class attribute_value_is:
    def __init__(self, locator, attribute, value):
        self.locator = locator
        self.attribute = attribute
        self.value = value
    def __call__(self, driver):
        element = driver.find_element(locator)
        return element.get_attribute(attribute) == value
C
def attribute_value_is(locator, attribute, value):
    element = driver.find_element(*locator)
    return element.get_attribute(attribute) == value
D
def attribute_value_is(locator, attribute, value):
    element = driver.find_element(locator)
    return element.get_attribute(attribute) == value
Attempts:
2 left
💡 Hint
Custom expected conditions must be callable classes with __call__ and use *self.locator.

Practice

(1/5)
1. What is the main purpose of using WebDriverWait in Selenium tests?
easy
A. To automatically retry failed tests
B. To pause the test for a fixed amount of time regardless of conditions
C. To speed up the test execution by skipping waits
D. To wait explicitly until a certain condition is met or timeout occurs

Solution

  1. 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.
  2. Step 2: Compare with other wait types

    Unlike fixed waits, explicit waits respond to page conditions, not just time delays.
  3. Final Answer:

    To wait explicitly until a certain condition is met or timeout occurs -> Option D
  4. Quick Check:

    Explicit wait = wait for condition [OK]
Hint: Explicit waits wait for conditions, not fixed times [OK]
Common Mistakes:
  • Confusing explicit wait with fixed sleep
  • Thinking it speeds up tests
  • Assuming it retries tests automatically
2. Which of the following is the correct syntax to create a WebDriverWait object with a 10-second timeout in Python?
easy
A. wait = WebDriverWait(driver, 10)
B. wait = WebDriverWait(10, driver)
C. wait = WebDriverWait(driver, timeout=10s)
D. wait = WebDriverWait(driver, "10")

Solution

  1. Step 1: Recall WebDriverWait constructor parameters

    The constructor takes the driver first, then the timeout as an integer or float in seconds.
  2. 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.
  3. Final Answer:

    wait = WebDriverWait(driver, 10) -> Option A
  4. Quick Check:

    WebDriverWait(driver, seconds) [OK]
Hint: Driver first, then timeout as number [OK]
Common Mistakes:
  • Swapping driver and timeout parameters
  • Using string instead of number for timeout
  • Adding units like 's' in timeout
3. What will be the output of the following code snippet if the element with id 'submit' appears after 3 seconds?
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)
medium
A. Element found: False
B. Element found: True
C. TimeoutException error
D. SyntaxError

Solution

  1. 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.
  2. 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'.
  3. Final Answer:

    Element found: True -> Option B
  4. Quick Check:

    Element appears before timeout = True [OK]
Hint: If element appears before timeout, wait.until returns it [OK]
Common Mistakes:
  • Assuming wait.until returns False if element appears
  • Expecting TimeoutException when element appears in time
  • Confusing syntax errors with runtime behavior
4. Identify the error in this code snippet using WebDriverWait:
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located(By.ID, 'username'))
medium
A. Missing tuple around locator in presence_of_element_located
B. Timeout value should be a string, not integer
C. WebDriverWait requires a third parameter for polling
D. EC.presence_of_element_located does not exist

Solution

  1. Step 1: Check expected condition usage

    The method presence_of_element_located expects a single argument: a tuple like (By.ID, 'username').
  2. Step 2: Identify the mistake in argument format

    The code passes By.ID and 'username' as separate arguments, missing the tuple, causing a TypeError.
  3. Final Answer:

    Missing tuple around locator in presence_of_element_located -> Option A
  4. Quick Check:

    Locator must be tuple (By.ID, 'value') [OK]
Hint: Always wrap locator in a tuple for expected conditions [OK]
Common Mistakes:
  • Passing locator arguments separately instead of tuple
  • Using string timeout instead of number
  • Thinking polling parameter is mandatory
5. You want to wait until either a button with id 'save' is clickable or a text element with id 'status' contains the text 'Ready', whichever happens first. Which approach correctly uses explicit waits to handle this?
hard
A. Use implicit wait instead of explicit wait for both elements
B. Use two separate WebDriverWait calls one after another for each condition
C. Use WebDriverWait with a custom expected condition combining both checks using a lambda function
D. Use time.sleep() to wait a fixed time before checking both elements

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Use WebDriverWait with a custom expected condition combining both checks using a lambda function -> Option C
  4. Quick Check:

    Custom combined condition = fastest wait [OK]
Hint: Combine conditions in one wait with lambda [OK]
Common Mistakes:
  • Waiting sequentially causing longer delays
  • Using implicit wait for condition checks
  • Using fixed sleep instead of waits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard