Automation tool (Selenium) · Java
Selenium exceptions and debugging
Stale, missing and intercepted elements, and how to debug them.
Learn: Selenium exceptions in Java and how to debug them7 minute read
An exception is the error object Java throws when something goes wrong. Selenium throws its own exceptions, and each name points at a cause: NoSuchElementException means the lookup found nothing, TimeoutException means a wait ran out. "Which exceptions have you faced, and how did you fix them?" is asked in almost every automation interview. A strong answer names the exception, the real cause, and a fix that removes the cause instead of hiding the error.
The hierarchy
Every Selenium exception extends WebDriverException, which extends RuntimeException. That makes them unchecked: the compiler never forces you to catch them or to declare them with throws. NotFoundException is the parent of NoSuchElementException, NoSuchFrameException, NoSuchWindowException and NoAlertPresentException. That is why WebDriverWait, which ignores NotFoundException, keeps polling through a missing element.
One import trap: java.util.NoSuchElementException also exists. With wildcard imports of both packages (import java.util.*; brings in every class of that package) the code does not compile, and catching the wrong one never catches Selenium's error. Import Selenium's class by name.
The common ones, with cause and fix
NoSuchElementException: the locator matched nothing. The locator is wrong, the element has not loaded yet, or it sits inside a frame or shadow root. Test the locator in DevTools, add an explicit wait, or switch frames.InvalidSelectorException: the locator itself is broken, such as an XPath with a missing bracket. Fix the syntax.TimeoutException: an explicit wait ran out. Read what it waited for. The condition may be wrong (visible when the element is only present), or the app really is slow, which may be a bug.StaleElementReferenceException: the element you hold was removed or replaced. Find it again after the change.ElementClickInterceptedException: another element, such as a spinner (a loading icon), banner or sticky header (a header that stays at the top while you scroll), would receive the click. Wait for it to go, close it, or scroll.ElementNotInteractableException: the element is hidden or cannot be used. Often the locator matches a hidden copy first.NoSuchFrameException,NoSuchWindowException: a wrong frame name, or a closed window you never switched away from.UnhandledAlertException: a command ran while an alert was open.SessionNotCreatedException: the browser could not start, often because driver and browser versions do not match.
Stale elements in detail
A WebElement is a reference to one node (one element) in the page's element tree, not a saved locator. When the page reloads, or a front-end framework (page code, such as React or Angular, that redraws parts of the page) redraws that part, the node is replaced. The old reference goes stale, even if a new element with the same id appears.
WebElement row = driver.findElement(By.cssSelector("#orders tbody tr"));
driver.findElement(By.id("sort-by-date")).click();
new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.stalenessOf(row)); // old table gone
WebElement fresh = driver.findElement(By.cssSelector("#orders tbody tr"));
Habits that prevent it: keep By locators in page objects and look elements up inside each method, and find again after any action that changes the page.
A debugging routine, step by step
- Read the exception type and the line. The type tells you which area to look at.
- Look at the failure screenshot: is the page in the state you expected?
- Check the locator in DevTools for exactly one match.
- Check the context: frame, window, alert.
- Check timing: is there a wait for the right condition?
- Run it several times. If it passes only sometimes, it is flaky; look for timing or shared test data.
How to answer in an interview
Pick three or four exceptions you really know. For each, give the cause and the fix in one sentence. Then tell one short real example: the exception, what you found, what you changed. Finish with your rule: fix causes, do not swallow exceptions.
Common mistakes
- Wrapping actions in a
try/catchthat ignores the error. - Adding
Thread.sleepuntil the failure goes away. - Switching every failing click to a JavaScript click, which can hide real bugs.
- Mixing up
NoSuchElementException(valid locator, no match) andInvalidSelectorException(broken locator).
Recap
Selenium exceptions are unchecked and share the WebDriverException parent. Each name points at a cause: locator, timing, context or page change. Read the type, check the screenshot, test the locator, check the context and the wait, and fix the cause.
12 questions0/12 solved
The first 2 questions of each topic are free. Pro+ opens every question.