Debug School

rakesh kumar
rakesh kumar

Posted on

Explain StaleElementReferenceException exception in selenium

StaleElementReferenceException in Selenium occurs when a previously located web element is no longer part of the DOM (Document Object Model) or has become "stale" due to a page refresh or navigation. This exception typically happens in dynamic web applications where elements are frequently updated. Here are different scenarios when a StaleElementReferenceException can occur, along with coding examples:

Page Refresh

DOM Update:

Navigation to a New Page

Element Update via AJAX

Elements in a Loop

Manual DOM Updates

Page Refresh:

This scenario occurs when you've located an element, but the page is refreshed or navigated to a different URL.

from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    element = driver.find_element_by_id("some-element")
    driver.refresh()  # The element becomes stale
    element.click()   # Raises StaleElementReferenceException
except StaleElementReferenceException as e:
    print(f"StaleElementReferenceException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

DOM Update:

In this scenario, the element you located initially has been modified or removed from the DOM by JavaScript or AJAX calls.

from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    element = driver.find_element_by_id("dynamic-element")
    # Some dynamic action happens, making the element stale
    element.click()  # Raises StaleElementReferenceException
except StaleElementReferenceException as e:
    print(f"StaleElementReferenceException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Navigation to a New Page:

When you locate an element on one page, but then navigate to a different page, the previously located element becomes stale.

from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com/page1")
    element = driver.find_element_by_id("some-element")
    driver.get("https://example.com/page2")  # Navigation to a new page
    element.click()   # Raises StaleElementReferenceException
except StaleElementReferenceException as e:
    print(f"StaleElementReferenceException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Element Update via AJAX:

If an element is dynamically updated via AJAX calls, the reference to it becomes stale.

from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    element = driver.find_element_by_id("ajax-updated-element")
    # An AJAX call updates the element, making it stale
    element.click()   # Raises StaleElementReferenceException
except StaleElementReferenceException as e:
    print(f"StaleElementReferenceException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Elements in a Loop:

When iterating through a list of elements and the DOM changes during the iteration, it can lead to a StaleElementReferenceException.

from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    elements = driver.find_elements_by_class_name("dynamic-element")
    for element in elements:
        # Some dynamic action happens, making the element stale
        element.click()   # Raises StaleElementReferenceException
except StaleElementReferenceException as e:
    print(f"StaleElementReferenceException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Manual DOM Updates:

If you manipulate the DOM manually (e.g., via JavaScript) and the element is affected, it can become stale.

from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    element = driver.find_element_by_id("some-element")
    # Manipulating the DOM, causing the element to become stale
    driver.execute_script("document.getElementById('some-element').remove()")
    element.click()   # Raises StaleElementReferenceException
except StaleElementReferenceException as e:
    print(f"StaleElementReferenceException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

To handle StaleElementReferenceException, it's important to re-locate the element after it becomes stale, such as by re-finding it using its locator or using a new WebDriverWait to ensure the element is available before interacting with it.

Top comments (0)