A custom expected condition is a user-defined function or class that waits for a specific condition to be true before proceeding, extending Selenium's built-in wait capabilities.
Click to reveal answer
beginner
Why use custom expected conditions instead of built-in ones?
Custom expected conditions allow you to wait for unique or complex scenarios that built-in conditions do not cover, making tests more reliable and tailored to your app.
Click to reveal answer
intermediate
How do you create a custom expected condition using a class in Selenium Python?
Create a class with a __call__ method that takes a driver and returns a truthy value when the condition is met or False otherwise. This class can then be used with WebDriverWait.
Click to reveal answer
beginner
What should a custom expected condition return when the condition is not met?
It should return False or None to indicate the wait should continue until the timeout or the condition becomes true.
Click to reveal answer
intermediate
Give an example of a simple custom expected condition function in Selenium Python.
Example:
def element_has_text(locator, text):
def _predicate(driver):
element = driver.find_element(*locator)
return element.text == text
return _predicate
Click to reveal answer
What does a custom expected condition in Selenium Python typically return when the condition is met?
AFalse
BA truthy value or the element
CAn exception
DNone
✗ Incorrect
When the condition is met, the custom expected condition returns a truthy value, often the element found, so WebDriverWait knows to stop waiting.
Which method must a class implement to be used as a custom expected condition in Selenium Python?
Afind_element
B__init__
Cwait
D__call__
✗ Incorrect
The __call__ method allows the class instance to be called like a function, which WebDriverWait uses to check the condition.
Why might you write a custom expected condition instead of using built-in ones?
ATo handle unique waiting scenarios not covered by built-in conditions
BBecause built-in conditions are deprecated
CTo avoid using WebDriverWait
DTo speed up test execution
✗ Incorrect
Custom expected conditions let you wait for specific, complex conditions unique to your application.
What should a custom expected condition return if the condition is not yet met?
AAn exception
BTrue
CFalse or None
DThe driver object
✗ Incorrect
Returning False or None tells WebDriverWait to keep waiting until the timeout or the condition becomes true.
Which Selenium class is commonly used with custom expected conditions to wait for conditions?
AWebDriverWait
BActionChains
CSelect
DChromeOptions
✗ Incorrect
WebDriverWait repeatedly calls the expected condition until it returns a truthy value or times out.
Explain how to create and use a custom expected condition in Selenium Python.
Think about how Selenium waits for elements or states.
You got /4 concepts.
Describe why custom expected conditions improve test reliability.
Consider real-life waiting situations that built-in waits can't handle.
You got /4 concepts.
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
Step 1: Understand built-in expected conditions
Built-in expected conditions cover common waits like element visibility or clickability.
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.
Final Answer:
To wait for a specific condition that is not covered by built-in expected conditions -> Option C
Quick Check:
Custom expected conditions = wait for custom needs [OK]
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
Step 1: Check usage of By.ID in the code
The code uses By.ID but does not import By, causing a NameError.
Step 2: Verify other parts of the code
Returning element.text is valid for expected conditions; inheritance from WebDriverWait is not required.
Final Answer:
Missing import for By from selenium.webdriver.common.by -> Option A
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
Step 1: Understand the requirement
We want to wait until the text contains the word 'Complete' anywhere inside.
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.
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.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.