Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a custom expected condition that waits for an element to be visible.
Selenium Python
class element_visible: def __init__(self, locator): self.locator = locator def __call__(self, driver): element = driver.find_element(*self.locator) return element if element.is_displayed() [1] None
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' instead of 'or' causes syntax errors.
Returning the element without checking visibility.
Returning True/False instead of element or None.
✗ Incorrect
The 'or' operator returns the element if it is displayed; otherwise, it returns None, which is the expected behavior for a custom expected condition.
2fill in blank
mediumComplete the code to define a custom expected condition that waits for the page title to contain a specific word.
Selenium Python
class title_contains_word: def __init__(self, word): self.word = word def __call__(self, driver): return self.word in driver.title [1] False
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'or False' which always returns True.
Returning the expression without ensuring a boolean result.
Using 'if' or 'else' incorrectly in a return statement.
✗ Incorrect
Using 'and False' ensures the expression returns True if the word is in the title, otherwise False, which is required for expected conditions.
3fill in blank
hardFix the error in the custom expected condition that waits for an element's text to equal a given value.
Selenium Python
class text_to_be_equal: def __init__(self, locator, text): self.locator = locator self.text = text def __call__(self, driver): element = driver.find_element(*self.locator) return element.text == self.text [1] element
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'or element' which returns element even if text does not match.
Returning True/False instead of element or False.
Using 'if' or 'else' incorrectly in return.
✗ Incorrect
Using 'and element' returns the element if the text matches, otherwise returns False, which is the expected behavior for expected conditions.
4fill in blank
hardFill both blanks to create a custom expected condition that waits for an element to be clickable.
Selenium Python
from selenium.webdriver.support import expected_conditions as EC class element_to_be_clickable_custom: def __init__(self, locator): self.locator = locator def __call__(self, driver): element = driver.find_element(*self.locator) return element.is_enabled() [1] element.is_displayed() [2] element
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'or' which returns element if only one condition is true.
Using 'if' or 'else' in return statement.
Returning True/False instead of element or None.
✗ Incorrect
Both conditions must be true for the element to be clickable, so 'and' is used to combine them and return the element if true.
5fill in blank
hardFill all three blanks to create a custom expected condition that waits for an element's attribute to have a specific value.
Selenium Python
class attribute_value_is: def __init__(self, locator, attribute, value): self.locator = locator self.attribute = attribute self.value = value def __call__(self, driver): element = driver.find_element(*self.locator) attr_value = element.get_attribute([1]) return attr_value == [2] [3] element
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for attribute or value.
Using 'or' which returns element even if values don't match.
Returning True/False instead of element or None.
✗ Incorrect
We get the attribute using self.attribute, compare it to self.value, and use 'and' to return the element if the condition is true.