0
0
Selenium Pythontesting~10 mins

StaleElementReferenceException handling in Selenium Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AStaleElementReferenceException
BNoSuchElementException
CTimeoutException
DElementNotInteractableException
Attempts:
3 left
💡 Hint
Common Mistakes
Catching the wrong exception like NoSuchElementException instead of StaleElementReferenceException.
Not importing the exception before using it.
2fill in blank
medium

Complete 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'
ABy.ID
BBy.CLASS_NAME
CBy.XPATH
DBy.NAME
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.
3fill in blank
hard

Fix 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'
AElementClickInterceptedException
BTimeoutException
CNoSuchElementException
DStaleElementReferenceException
Attempts:
3 left
💡 Hint
Common Mistakes
Catching the wrong exception causing the retry loop to fail.
Not breaking the loop after successful click.
4fill in blank
hard

Fill 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'
ABy.ID
BTimeoutException
CStaleElementReferenceException
DBy.CLASS_NAME
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong locator strategy.
Catching the wrong exception.
5fill in blank
hard

Fill 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'
ABy.ID
BStaleElementReferenceException
C2
DBy.XPATH
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong locator strategy for the wait condition.
Not waiting before retrying after catching the exception.