Automation tool (Selenium) · Python
Selenium exceptions and debugging
Stale, missing and intercepted elements, and how to debug them.
Learn: Selenium exceptions in Python and how to debug them7 minute read
An exception is the error Python raises when something goes wrong. Selenium raises its own exceptions, and each name points at a cause: NoSuchElementException means the lookup found nothing, TimeoutException means a wait ran out. Interviewers ask which ones you have met and how you dealt with them. A good answer names the exception, explains the real cause and shows a fix that removes it. Reading a Python traceback quickly is part of the skill.
Where they live
All Selenium exceptions are in selenium.common.exceptions and derive from WebDriverException. Import the specific ones you handle:
from selenium.common.exceptions import (
NoSuchElementException, StaleElementReferenceException, TimeoutException)
Selenium's TimeoutException is not Python's built-in TimeoutError, so except TimeoutError will not catch a wait timeout.
Reading a traceback
A traceback is the error report Python prints. Read it from the bottom up. The last line names the exception and its message; the lines above show the chain of calls, and the first line that points into your own file is where to look. Selenium messages include the locator that was sent. Python sends By.ID to the driver as a CSS selector, so By.ID, "missingField" appears in the message as [id="missingField"].
The common ones, with cause and fix
NoSuchElementException: nothing matched. The locator is wrong, the content has not loaded, or the element is in a frame or shadow root. Retrying a wrong locator never helps; fix it or wait for the right condition.InvalidSelectorException: the locator string itself is broken.TimeoutException: an explicit wait ran out. Compare the condition with the page in the screenshot.StaleElementReferenceException: the element was replaced after a refresh or redraw; find it again.ElementClickInterceptedException: something covers the element, such as a loading icon or a sticky header (a header that stays at the top while you scroll). Wait for the overlay to go, close banners, or scroll.ElementNotInteractableException: hidden or unusable, often a hidden duplicate matched first.NoSuchFrameException,NoSuchWindowException: wrong frame, or a closed window you did not switch away from.UnexpectedAlertPresentException: an alert blocked another command.SessionNotCreatedException: the browser could not start; check driver and browser versions.
Stale elements in detail
A WebElement points to one node in the page's element tree. Refreshing, navigating, sorting a table or an AJAX update (data loaded in the background) can replace that node:
field = driver.find_element(By.ID, "preFilledTextField")
driver.refresh()
try:
field.get_attribute("value")
except StaleElementReferenceException:
field = driver.find_element(By.ID, "preFilledTextField") # find again
print(field.get_attribute("value"))
In a framework the cure is structural: page objects keep locator tuples and find elements inside each method, so references never live long. To wait for a change, use EC.staleness_of(old_element), then wait for the new content.
A debugging routine, step by step
- Read the last traceback line and the first line in your own code.
- Open the failure screenshot: is the page in the expected state?
- Test the locator in DevTools; it should match exactly one element.
- Check the context: frame, window, alert.
- Check timing: is there an explicit wait for the right condition?
- Run the test several times. If it fails only sometimes, it is flaky; look for timing or shared data.
How to answer in an interview
Choose three or four exceptions you know well and give cause and fix for each in one sentence. Then tell one real example in a few sentences: the exception, what you found, what you changed. A short true story beats a long list.
Common mistakes
- A bare
except Exception: passthat hides the failure. - Adding
time.sleepuntil the error goes away. - Switching every click to JavaScript, which can hide real product bugs.
- Catching
WebDriverExceptioneverywhere instead of the one specific exception you expect.
Recap
All Selenium exceptions live in selenium.common.exceptions. Each name points at a cause: locator, timing, context or page change. Read the traceback from the bottom, check the screenshot, test the locator, check the context and the wait, and fix the cause.
13 questions0/13 solved
The first 2 questions of each topic are free. Pro+ opens every question.