Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Expected Conditions 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 expected condition?
Consider the following Selenium custom expected condition class. What will be the output of the test assertion if the element with id 'status' has text 'Ready'?
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_text_is_ready:
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        return element.text == 'Ready'

# Usage example:
# wait = WebDriverWait(driver, 10)
# result = wait.until(element_text_is_ready((By.ID, 'status')))
# print(result)
ATimeoutException
BFalse
CTrue
DAttributeError
Attempts:
2 left
💡 Hint
Think about what the __call__ method returns when the element text matches 'Ready'.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies a custom expected condition?
You have a custom expected condition that returns the element if visible, else False. Which assertion correctly checks that the element is found and visible?
Selenium Python
class element_is_visible:
    def __init__(self, locator):
        self.locator = locator

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

# wait = WebDriverWait(driver, 10)
# result = wait.until(element_is_visible((By.CSS_SELECTOR, '.btn')))
Aassert result == False
Bassert result is not False
Cassert result is None
Dassert result == True
Attempts:
2 left
💡 Hint
Remember the custom condition returns the element or False, not a boolean True.
🔧 Debug
advanced
2:00remaining
Why does this custom expected condition cause a TimeoutException?
This custom expected condition is intended to wait until an element's attribute 'data-status' equals 'complete'. Why does it always cause a TimeoutException?
Selenium Python
class attribute_value_is_complete:
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        return element.get_attribute('data-status') == 'complete'

# wait = WebDriverWait(driver, 5)
# wait.until(attribute_value_is_complete((By.ID, 'task')))
AAttribute 'data-status' does not exist on element
BTimeout is too short to wait for condition
CThe __call__ method returns None instead of boolean
DIncorrect unpacking of locator tuple in find_element
Attempts:
2 left
💡 Hint
Check how the locator tuple is used in find_element.
❓ framework
advanced
2:00remaining
Which custom expected condition class correctly waits for an element's CSS class to include 'active'?
Select the class that correctly implements a custom expected condition to wait until the element's class attribute contains the word 'active'.
A
class element_has_active_class:
    def __init__(self, locator):
        self.locator = locator
    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        return 'active' in element.get_attribute('class')
B
class element_has_active_class:
    def __init__(self, locator):
        self.locator = locator
    def __call__(self, driver):
        element = driver.find_element(self.locator)
        return element.get_attribute('class') == 'active'
C
class element_has_active_class:
    def __init__(self, locator):
        self.locator = locator
    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        return element.get_attribute('class').contains('active')
D
class element_has_active_class:
    def __init__(self, locator):
        self.locator = locator
    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        return element.get_attribute('class') == ['active']
Attempts:
2 left
💡 Hint
Check correct locator unpacking and string containment syntax in Python.
🧠 Conceptual
expert
2:00remaining
What is the main advantage of using custom expected conditions in Selenium tests?
Why should testers create custom expected conditions instead of only using built-in ones?
AThey allow waiting for complex or specific conditions not covered by built-in ones
BThey reduce test execution time by skipping waits
CThey automatically fix flaky tests without code changes
DThey replace the need for assertions in tests
Attempts:
2 left
💡 Hint
Think about flexibility and test reliability.

Practice

(1/5)
1. What is the main purpose of creating a custom expected condition in Selenium Python?
easy
A. To change the browser window size automatically
B. To speed up the browser loading time
C. To wait for a specific condition that is not covered by built-in expected conditions
D. To generate test reports automatically

Solution

  1. Step 1: Understand built-in expected conditions

    Built-in expected conditions cover common waits like element visibility or clickability.
  2. Step 2: Identify the need for custom conditions

    When a specific wait is not available, custom expected conditions let you define exactly what to wait for.
  3. Final Answer:

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

    Custom expected conditions = wait for custom needs [OK]
Hint: Custom conditions handle waits built-ins can't cover [OK]
Common Mistakes:
  • Thinking custom conditions speed up browser loading
  • Confusing custom conditions with browser resizing
  • Assuming custom conditions generate reports
2. Which of the following is the correct way to define a custom expected condition class in Selenium Python?
easy
A. class MyCondition: def __call__(self, driver): return driver.title == 'Home'
B. def MyCondition(driver): return driver.title == 'Home'
C. class MyCondition: def call(self, driver): return driver.title == 'Home'
D. class MyCondition: def __init__(self, driver): return driver.title == 'Home'

Solution

  1. Step 1: Recall the special method for callable classes

    The method __call__ makes an instance callable like a function.
  2. Step 2: Check method signature and return

    The __call__ method takes driver and returns a boolean or element to wait for.
  3. Final Answer:

    class MyCondition:\n def __call__(self, driver):\n return driver.title == 'Home' -> Option A
  4. Quick Check:

    Custom condition class needs __call__ method [OK]
Hint: Use __call__ method to make class callable [OK]
Common Mistakes:
  • Using call instead of __call__ method
  • Defining __init__ to return a value
  • Defining a function instead of a class
3. Given this custom expected condition class, what will WebDriverWait(driver, 10).until(MyCondition()) return if the page title is 'Dashboard'?
class MyCondition:
    def __call__(self, driver):
        if driver.title == 'Dashboard':
            return True
        else:
            return False
medium
A. It will raise a TimeoutException immediately
B. It will return the driver object
C. It will return False immediately without waiting
D. It will wait until the title is 'Dashboard' and then return True

Solution

  1. Step 1: Understand WebDriverWait with custom condition

    WebDriverWait calls the condition repeatedly until it returns a truthy value or timeout.
  2. Step 2: Analyze the condition's return value

    If the title is 'Dashboard', __call__ returns True, so wait ends and returns True.
  3. Final Answer:

    It will wait until the title is 'Dashboard' and then return True -> Option D
  4. Quick Check:

    Condition returns True -> wait ends successfully [OK]
Hint: Wait ends when condition returns True [OK]
Common Mistakes:
  • Thinking it raises TimeoutException immediately
  • Assuming it returns False immediately
  • Confusing return value with driver object
4. Identify the error in this custom expected condition class:
class ElementHasText:
    def __call__(self, driver):
        element = driver.find_element(By.ID, 'msg')
        if element.text:
            return element.text
        else:
            return False
medium
A. Missing import for By from selenium.webdriver.common.by
B. The __call__ method should return True or False only, not text
C. The class should inherit from WebDriverWait
D. The find_element method should be find_elements

Solution

  1. Step 1: Check usage of By.ID in the code

    The code uses By.ID but does not import By, causing a NameError.
  2. Step 2: Verify other parts of the code

    Returning element.text is valid for expected conditions; inheritance from WebDriverWait is not required.
  3. Final Answer:

    Missing import for By from selenium.webdriver.common.by -> Option A
  4. Quick Check:

    Using By requires importing it first [OK]
Hint: Always import By before using it [OK]
Common Mistakes:
  • Assuming __call__ must return only True/False
  • Thinking class must inherit WebDriverWait
  • Confusing find_element with find_elements
5. You want to create a custom expected condition that waits until an element with ID 'status' has text containing the word 'Complete'. Which of these implementations is correct and efficient?
hard
A. class StatusComplete: def __call__(self, driver): element = driver.find_element(By.ID, 'status') return element.text.startswith('Complete')
B. class StatusComplete: def __call__(self, driver): element = driver.find_element(By.ID, 'status') return 'Complete' in element.text
C. class StatusComplete: def __call__(self, driver): element = driver.find_element(By.ID, 'status') if element.text == 'Complete': return True else: return False
D. class StatusComplete: def __call__(self, driver): element = driver.find_element(By.ID, 'status') return element.text != ''

Solution

  1. Step 1: Understand the requirement

    We want to wait until the text contains the word 'Complete' anywhere inside.
  2. Step 2: Analyze each option's condition

    class StatusComplete: def __call__(self, driver): element = driver.find_element(By.ID, 'status') return 'Complete' in element.text checks if 'Complete' is in element.text, which matches the requirement exactly and returns a boolean.
  3. Step 3: Compare other options

    class StatusComplete: def __call__(self, driver): element = driver.find_element(By.ID, 'status') if element.text == 'Complete': return True else: return False checks exact equality, which is too strict. class StatusComplete: def __call__(self, driver): element = driver.find_element(By.ID, 'status') return element.text.startswith('Complete') checks startswith, which is partial. class StatusComplete: def __call__(self, driver): element = driver.find_element(By.ID, 'status') return element.text != '' only checks non-empty text, which is too broad.
  4. Final Answer:

    class StatusComplete:\n def __call__(self, driver):\n element = driver.find_element(By.ID, 'status')\n return 'Complete' in element.text -> Option B
  5. Quick Check:

    Use 'in' to check substring presence [OK]
Hint: Use 'in' operator to check substring in text [OK]
Common Mistakes:
  • Checking exact equality instead of substring
  • Using startswith instead of contains
  • Checking only for non-empty text
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