Challenge - 5 Problems
Custom Wait Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
The custom condition returns the element only if the text matches, otherwise False to keep waiting.
✗ Incorrect
The custom wait condition waits until the element with ID 'status' contains the text 'Complete'. When found, it returns the element, so printing element.text outputs 'Complete'.
❓ assertion
intermediate1: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())
Attempts:
2 left
💡 Hint
The wait returns a boolean True or False, not a string or element.
✗ Incorrect
The lambda returns True when the button is enabled, so the variable button_enabled is True. The correct assertion checks for True explicitly.
🔧 Debug
advanced2: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'))
Attempts:
2 left
💡 Hint
WebDriverWait expects the condition to return a truthy value or the element, not just True.
✗ Incorrect
Returning True instead of the element causes wait.until to keep waiting because it expects a truthy value that is not just True but usually the element or a non-boolean truthy value.
❓ framework
advanced2: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?
Attempts:
2 left
💡 Hint
Fluent waits expect the condition to return the element or False to continue waiting.
✗ Incorrect
Option D returns the element when the text is found, which is a truthy value that stops the wait. Other options return booleans or just True/False, which may not stop the wait correctly.
🧠 Conceptual
expert1: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?
Attempts:
2 left
💡 Hint
Think about how waiting for conditions affects test reliability and speed.
✗ Incorrect
Custom waits wait dynamically for conditions, reducing flakiness and improving test speed by not waiting longer than necessary. Fixed sleeps always wait the full time, slowing tests and causing flakiness.