Python Selenium: Fixing Stale Element and NoSuchElement Errors
Learn why Python Selenium raises StaleElementReferenceException and NoSuchElementException, and how to fix them with explicit waits, retries, and stable locators.

When automating a dynamic web application with Python and Selenium, two exceptions are especially common: StaleElementReferenceException and NoSuchElementException. These errors typically come from timing and DOM changes: your code tries to interact with an element that is not present yet, or with a reference to an element that is no longer attached to the page. The practical fixes are explicit waits, retry logic, and robust locators that keep your script in sync with the page state.
Why StaleElementReferenceException Occurs
A StaleElementReferenceException is raised when a previously located element is no longer attached to the DOM. Selenium stores a reference to the element object, but the DOM can change between the time you find the element and the time you act on it. This often happens after a page re-renders, a JavaScript framework updates a section, or a user action triggers an AJAX response.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com")
button = driver.find_element(By.ID, "submit")
button.click()
# After the click, the page re-renders and the original element reference is invalid.
text = button.text # Raises StaleElementReferenceExceptionIn this example, clicking the button likely triggers a DOM update. The button variable still points to the old element, which is now detached. Any subsequent method call on that reference fails.
Why NoSuchElementException Occurs
A NoSuchElementException is thrown when Selenium cannot find an element matching the given locator at the moment of the search. The element may not exist yet because the page or a dynamic section is still loading, or it may be inside an iframe or shadow DOM that your current context doesn't cover.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com")
# If the page hasn't finished rendering, this element may not be present yet.
element = driver.find_element(By.CLASS_NAME, "dynamic-content") # Raises NoSuchElementExceptionUnlike a stale element, the element was never found in the first place. In this example the locator is valid but the element is not available yet; the same exception can also mean the locator does not match anything in the current DOM.
How Timing and DOM Changes Produce Both Errors
In real applications, these two exceptions often appear together in the same test run. A page may load a list, then a background request updates part of the DOM. If your script finds an element before the update, it becomes stale. If it searches after the update but before the new content is inserted, it gets a no-such-element error. In these scenarios, the exceptions are race conditions between your script's execution and the browser's rendering pipeline.
Consider a single-page application that fetches data from an API. The initial HTML contains a loading spinner, then the data replaces it. If you locate an element from the loading state and try to reuse it, you get a stale reference. If you look for the data container too early, you get a no-such-element error. The solution in both cases is to synchronize your script with the page state.
Using Explicit Waits to Align with Page State
When the cause is timing, the most reliable way to avoid both exceptions is to use explicit waits. WebDriverWait combined with expected_conditions tells Selenium to poll the DOM until a condition is met, rather than failing immediately.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
# Wait for the element to be present and visible before interacting.
element = wait.until(EC.visibility_of_element_located((By.ID, "dynamic-content")))
element.click()For stale elements, you can wait for a specific condition that indicates the DOM has settled. For example, after a click that triggers a re-render, wait for the element you need to become stale, then wait for the new version to appear.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Wait for the old element to become stale, then wait for the new one.
old_element = driver.find_element(By.ID, "content")
old_element.click()
WebDriverWait(driver, 10).until(EC.staleness_of(old_element))
new_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "content"))
)Explicit waits are preferred over implicit waits because they give you fine-grained control. An implicit wait applies a global timeout to every find_element call, which can slow down your script and mask real issues. Explicit waits target specific conditions and make your intent clear.
Retrying Element Lookups for Stale References
Even with explicit waits, you may occasionally encounter a stale reference if the DOM changes between the wait and the action. A common pattern is to catch StaleElementReferenceException and retry the lookup with a fresh reference.
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By
def click_with_retry(driver, locator, max_attempts=3):
for attempt in range(max_attempts):
try:
element = driver.find_element(*locator)
element.click()
return
except StaleElementReferenceException:
if attempt == max_attempts - 1:
raiseThis retry loop re-finds the element each time, so it always works with the latest DOM state. It is especially useful for elements that are frequently re-rendered, such as table rows or dynamic forms. The loop should have a reasonable maximum attempt count to avoid infinite loops and to fail fast when the page is genuinely broken.
Designing Locators That Resist Both Errors
Locator quality directly affects how often you see these exceptions. A fragile locator that depends on dynamic class names or index positions is more likely to fail when the page changes. Use stable attributes like id, name, or data-testid when available. Prefer CSS selectors over XPath for simple cases, and avoid absolute XPath expressions that break with any layout change.
# Fragile: relies on a dynamic class and position
# driver.find_element(By.XPATH, "//div[2]/span[3]")
# More robust: use a stable data attribute
element = driver.find_element(By.CSS_SELECTOR, "[data-testid='submit-button']")When you must use XPath, keep it relative and short. For example, //button[contains(@class, 'submit')] is more resilient than a full path from the root. Also, avoid relying on text content that may change between locales or versions.
Production Considerations for Reliable Selenium Runs
In a CI pipeline, flaky tests caused by stale and missing elements waste time and reduce confidence. To minimize these issues, treat waits as part of your test design, not an afterthought. Use explicit waits with timeouts that reflect the real performance of the application. A timeout that is too short causes false failures; one that is too long slows down the suite.
Logging is also important. When an exception occurs, capture the page source and a screenshot to understand the state at the moment of failure. This helps you decide whether the problem is a timing issue or a genuine application bug.
Finally, consider using the Page Object Model to centralize element locators and interaction logic. A page object encapsulates the locators and the actions, so when the UI changes, you update one class instead of every test. This reduces the chance of using an outdated locator and makes the test suite more maintainable over time.
from selenium.webdriver.common.by import By
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username = (By.ID, "username")
self.password = (By.ID, "password")
self.submit = (By.CSS_SELECTOR, "[data-testid='login-submit']")
def login(self, user, pwd):
self.driver.find_element(*self.username).send_keys(user)
self.driver.find_element(*self.password).send_keys(pwd)
self.driver.find_element(*self.submit).click()By combining explicit waits, retry logic, stable locators, and a clear page object structure, you can reduce the frequency of StaleElementReferenceException and NoSuchElementException to the point where they become rare, isolated incidents rather than daily blockers.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.