Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of creating a custom wait condition in Selenium with Python?
easy
A. To automatically retry failed tests
B. To speed up the test execution by skipping waits
C. To replace the WebDriverWait class completely
D. To wait for a specific condition that is not covered by built-in waits

Solution

  1. Step 1: Understand built-in waits limitations

    Built-in waits cover common cases but sometimes you need to wait for a unique condition.
  2. Step 2: Purpose of custom wait

    Custom waits let you define exactly what to wait for, improving test reliability.
  3. Final Answer:

    To wait for a specific condition that is not covered by built-in waits -> Option D
  4. Quick Check:

    Custom wait = wait for unique condition [OK]
Hint: Custom waits handle unique conditions not in built-in waits [OK]
Common Mistakes:
  • Thinking custom waits speed up tests
  • Believing custom waits replace WebDriverWait
  • Assuming custom waits retry tests automatically
2. Which of the following is the correct way to define a custom wait condition class in Selenium Python?
easy
A. class MyWait: def __call__(self, driver): return driver.find_element_by_id('myid')
B. class MyWait: def wait(self): return True
C. def MyWait(driver): return driver.find_element('id', 'myid')
D. class MyWait: def wait_for(self, driver): return False

Solution

  1. Step 1: Identify the special method for custom waits

    Custom wait classes must implement the __call__ method to be callable by WebDriverWait.
  2. Step 2: Check method signature and return

    Method __call__ takes driver and returns element or True when condition met.
  3. Final Answer:

    class MyWait:\n def __call__(self, driver):\n return driver.find_element_by_id('myid') -> Option A
  4. Quick Check:

    Custom wait uses __call__ method [OK]
Hint: Custom wait class must have __call__(self, driver) method [OK]
Common Mistakes:
  • Using method named wait instead of __call__
  • Defining function instead of class
  • Using wrong method signature
3. Given the code below, what will be the output if the element with id 'submit' appears after 3 seconds?
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

class WaitForSubmit:
    def __call__(self, driver):
        try:
            elem = driver.find_element(By.ID, 'submit')
            return elem
        except:
            return False

wait = WebDriverWait(driver, 5)
submit_button = wait.until(WaitForSubmit())
print(submit_button.tag_name)
medium
A. Prints False
B. Raises TimeoutException immediately
C. Prints the tag name of the 'submit' element after waiting
D. Raises NoSuchElementException

Solution

  1. Step 1: Understand the custom wait behavior

    The custom wait tries to find element with id 'submit'. If not found, returns False to keep waiting.
  2. Step 2: Analyze wait.until with timeout 5 seconds

    The element appears after 3 seconds, so wait.until returns the element before timeout.
  3. Final Answer:

    Prints the tag name of the 'submit' element after waiting -> Option C
  4. Quick Check:

    Element appears before timeout, so tag_name prints [OK]
Hint: Custom wait returns element or False; wait.until waits until element found [OK]
Common Mistakes:
  • Assuming immediate TimeoutException
  • Expecting print of False instead of element
  • Confusing exceptions raised
4. Identify the error in the following custom wait condition code:
class WaitForText:
    def __call__(self, driver):
        element = driver.find_element(By.ID, 'message')
        if element.text == 'Done':
            return True
        else:
            return None

wait = WebDriverWait(driver, 10)
wait.until(WaitForText())
medium
A. Returning None instead of False causes wait.until to stop waiting
B. Missing import for By causes NameError
C. Returning True instead of element causes error
D. Using find_element instead of find_elements causes exception

Solution

  1. Step 1: Check imports

    The code uses By.ID but does not import By, causing NameError at runtime.
  2. Step 2: Analyze return values

    Returning True or None is allowed; wait.until treats True as success and None as failure to continue waiting.
  3. Final Answer:

    Missing import for By causes NameError -> Option B
  4. Quick Check:

    By must be imported to avoid NameError [OK]
Hint: Always import By before using it in locators [OK]
Common Mistakes:
  • Thinking None stops waiting (it continues)
  • Assuming returning True causes error
  • Confusing find_element with find_elements
5. You want to wait until a button with id 'start' is both visible and enabled before clicking it. Which custom wait condition class correctly implements this?
hard
A. class WaitForStart: def __call__(self, driver): btn = driver.find_element(By.ID, 'start') if btn.is_displayed() and btn.is_enabled(): return btn return False
B. class WaitForStart: def __call__(self, driver): btn = driver.find_element(By.ID, 'start') if btn.is_displayed() or btn.is_enabled(): return True return False
C. class WaitForStart: def __call__(self, driver): btn = driver.find_element(By.ID, 'start') if btn.is_displayed() and btn.is_enabled(): return True return False
D. class WaitForStart: def __call__(self, driver): btn = driver.find_element(By.ID, 'start') if btn.is_enabled(): return btn return None

Solution

  1. Step 1: Check condition logic

    We need both visible and enabled, so use 'and' operator to check both.
  2. Step 2: Return element when condition met

    Return the button element so wait.until returns it for further actions.
  3. Final Answer:

    class WaitForStart:\n def __call__(self, driver):\n btn = driver.find_element(By.ID, 'start')\n if btn.is_displayed() and btn.is_enabled():\n return btn\n return False -> Option A
  4. Quick Check:

    Return element when visible and enabled [OK]
Hint: Return element only if visible AND enabled, else False [OK]
Common Mistakes:
  • Using 'or' instead of 'and' for conditions
  • Returning True instead of element
  • Returning None instead of False
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