Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to catch the StaleElementReferenceException.
Selenium Python
from selenium.common.exceptions import [1] try: element.click() except [1]: print("Element is stale, retrying...")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Catching the wrong exception like NoSuchElementException instead of StaleElementReferenceException.
Not importing the exception before using it.
✗ Incorrect
The StaleElementReferenceException is the correct exception to catch when the element is no longer attached to the DOM.
2fill in blank
mediumComplete the code to re-find the element after catching StaleElementReferenceException.
Selenium Python
from selenium.webdriver.common.by import By try: element.click() except StaleElementReferenceException: element = driver.find_element([1], 'submit-button') element.click()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a locator that does not match the element's attribute.
Not re-finding the element after catching the exception.
✗ Incorrect
Using By.ID is a common and reliable way to locate elements by their unique ID attribute.
3fill in blank
hardFix the error in the retry loop to handle stale element properly.
Selenium Python
for _ in range(3): try: element.click() break except [1]: element = driver.find_element(By.ID, 'submit-button')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Catching the wrong exception causing the retry loop to fail.
Not breaking the loop after successful click.
✗ Incorrect
The retry loop should catch StaleElementReferenceException to re-find the element and retry the click.
4fill in blank
hardFill both blanks to create a function that retries clicking an element safely.
Selenium Python
def safe_click(driver, locator): for _ in range(3): try: element = driver.find_element([1], locator) element.click() return True except [2]: continue return False
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong locator strategy.
Catching the wrong exception.
✗ Incorrect
The function uses By.ID to find the element and catches StaleElementReferenceException to retry clicking.
5fill in blank
hardFill all three blanks to handle stale element exception with explicit wait before retry.
Selenium Python
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def click_with_wait(driver, locator): wait = WebDriverWait(driver, 10) for _ in range(3): try: element = wait.until(EC.element_to_be_clickable(([1], locator))) element.click() return True except [2]: import time time.sleep([3]) return False
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong locator strategy for the wait condition.
Not waiting before retrying after catching the exception.
✗ Incorrect
The function waits for the element located by By.XPATH to be clickable, catches StaleElementReferenceException, and waits 2 seconds before retrying.