0
0
Selenium Pythontesting~20 mins

Custom wait conditions in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Wait 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 custom wait condition code?
Consider the following Selenium Python code using a custom wait condition. What will be printed after the wait?
Selenium Python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

class element_has_text:
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text

    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        if self.text in element.text:
            return element
        else:
            return False

# Assume driver is a valid WebDriver instance
wait = WebDriverWait(driver, 10)
result = wait.until(element_has_text((By.ID, 'status'), 'Complete'))
print(result.text)
AComplete
BTimeoutException is raised
CEmpty string printed
DAttributeError due to missing text attribute
Attempts:
2 left
💡 Hint
The custom condition returns the element only if the text matches, otherwise False to keep waiting.
assertion
intermediate
1:30remaining
Which assertion correctly verifies a custom wait condition?
You have a custom wait condition that returns True when a button becomes enabled. Which assertion correctly checks this after waiting?
Selenium Python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, 5)
button_enabled = wait.until(lambda d: d.find_element(By.ID, 'submit').is_enabled())
Aassert button_enabled == 'enabled'
Bassert button_enabled.text == 'enabled'
Cassert button_enabled is True
Dassert button_enabled is not None
Attempts:
2 left
💡 Hint
The wait returns a boolean True or False, not a string or element.
🔧 Debug
advanced
2:30remaining
Why does this custom wait condition cause a TimeoutException?
Examine the custom wait condition below. Why does it always cause a TimeoutException even if the element is present?
Selenium Python
class element_has_class:
    def __init__(self, locator, class_name):
        self.locator = locator
        self.class_name = class_name

    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        if self.class_name in element.get_attribute('class'):
            return element
        else:
            return False

wait = WebDriverWait(driver, 5)
wait.until(element_has_class((By.CSS_SELECTOR, '.my-element'), 'active'))
AThe __call__ method returns True instead of the element, so wait.until never stops
BThe wait timeout is too short to find the element
CThe class_name check is case-sensitive and fails if class attribute is uppercase
DThe locator uses a CSS selector with a dot, causing find_element to fail
Attempts:
2 left
💡 Hint
WebDriverWait expects the condition to return a truthy value or the element, not just True.
framework
advanced
2:30remaining
Which custom wait condition implementation best supports fluent waits?
You want a custom wait condition class that supports fluent waits by returning the element when ready or False otherwise. Which implementation is best?
A
class WaitForText:
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text
    def __call__(self, driver):
        try:
            element = driver.find_element(*self.locator)
            return element.text == self.text
        except:
            return False
B
class WaitForText:
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text
    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        if self.text == element.text:
            return True
        else:
            return False
C
class WaitForText:
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text
    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        return self.text in element.text
D
class WaitForText:
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text
    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        return element if self.text in element.text else False
Attempts:
2 left
💡 Hint
Fluent waits expect the condition to return the element or False to continue waiting.
🧠 Conceptual
expert
1:30remaining
What is the main advantage of using custom wait conditions in Selenium tests?
Why should testers prefer custom wait conditions over fixed sleep delays in Selenium automation?
ACustom waits make tests run slower by adding unnecessary delays
BCustom waits reduce test flakiness by waiting only as long as needed for specific conditions
CCustom waits are easier to write than fixed sleep calls
DCustom waits guarantee tests pass regardless of application state
Attempts:
2 left
💡 Hint
Think about how waiting for conditions affects test reliability and speed.