0
0
Selenium Pythontesting~20 mins

StaleElementReferenceException handling in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stale Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding StaleElementReferenceException

What does a StaleElementReferenceException mean in Selenium WebDriver?

AThe element is no longer attached to the current page's DOM.
BThe element is visible but disabled for interaction.
CThe browser window has crashed during the test execution.
DThe element locator is incorrect and cannot be found.
Attempts:
2 left
💡 Hint

Think about what happens if the page changes after you find an element.

Predict Output
intermediate
2:00remaining
Output of code causing StaleElementReferenceException

What will be the output or result of running the following Selenium Python code snippet?

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Chrome()
browser.get('https://example.com')
element = browser.find_element(By.TAG_NAME, 'h1')
browser.refresh()
print(element.text)
browser.quit()
APrints the text of the button successfully.
BPrints an empty string without error.
CRaises NoSuchElementException at find_element line.
DRaises StaleElementReferenceException when accessing element.text.
Attempts:
2 left
💡 Hint

Consider what happens to the element after the page refresh.

assertion
advanced
1:30remaining
Correct assertion to handle stale element

Which assertion correctly verifies that a button's text is 'Submit' after handling a stale element?

Selenium Python
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By

try:
    button = driver.find_element(By.ID, 'submit-btn')
    text = button.text
except StaleElementReferenceException:
    button = driver.find_element(By.ID, 'submit-btn')
    text = button.text
Aassert button.text == 'Submit' and text == 'Submit'
Bassert text == 'Submit'
Cassert text != 'Submit'
Dassert button is not None
Attempts:
2 left
💡 Hint

Focus on the variable holding the button text after retry.

🔧 Debug
advanced
1:30remaining
Identify the cause of StaleElementReferenceException

Given the following test code, why does it raise a StaleElementReferenceException?

Selenium Python
element = driver.find_element(By.CSS_SELECTOR, '.item')
driver.execute_script('document.querySelector(".item").remove()')
print(element.text)
AThe element was removed from the DOM by JavaScript before accessing text.
BThe driver.execute_script method is not supported.
CThe CSS selector is invalid and cannot find the element.
DThe element variable was never assigned.
Attempts:
2 left
💡 Hint

Think about what happens when the element is removed from the page.

framework
expert
2:00remaining
Best practice to handle StaleElementReferenceException in test framework

Which approach is best to handle StaleElementReferenceException in a Selenium test framework to make tests more reliable?

AUse fixed time sleep after every element find to avoid staleness.
BIgnore the exception and continue test execution without element access.
CWrap element access in a retry loop that refinds the element on exception.
DCatch the exception and fail the test immediately without retry.
Attempts:
2 left
💡 Hint

Think about how to recover from dynamic page changes gracefully.