🚨 Top 10 Selenium Exceptions Every Automation Tester Should Know

Dinesh Y SDinesh Y S
3 min read

If you're working with Selenium WebDriver in Java, you've probably run into exceptions that seem cryptic at first. Understanding these exceptions not only helps you debug faster but also improves your test automation design.

In this post, I'll walk you through the top 10 Selenium exceptions, why they happen, and how to fix or prevent them. Let’s dive in! 👇

1️⃣ NoSuchElementException

💣 Cause:

Trying to find an element that doesn’t exist in the DOM.

💡 Fix:

Use explicit waits to ensure the element is present before accessing it.

javaCopyEditWebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));

2️⃣ NoSuchWindowException

💣 Cause:

Trying to switch to a window that has been closed or never existed.

💡 Fix:

Always loop through driver.getWindowHandles() and validate the window before switching.


3️⃣ NoSuchFrameException

💣 Cause:

Trying to switch to a frame that’s not available.

💡 Fix:

Use the frame name/index properly, or wait until the frame is available.

javaCopyEditwait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("frameName"));

4️⃣ TimeoutException

💣 Cause:

Explicit or implicit wait ran out of time while waiting for an element or condition.

💡 Fix:

Increase wait duration or use better locators.


5️⃣ ElementNotInteractableException

💣 Cause:

The element is present in the DOM but not interactable (like hidden or disabled).

💡 Fix:

Wait until the element is clickable.

javaCopyEditwait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));

6️⃣ StaleElementReferenceException

💣 Cause:

You found an element, but before using it, the DOM changed and invalidated the reference.

💡 Fix:

Re-locate the element just before interacting with it.


7️⃣ InvalidSelectorException

💣 Cause:

Your XPath or CSS selector is syntactically wrong.

💡 Fix:

Double-check the selector syntax using browser dev tools.


8️⃣ ElementClickInterceptedException

💣 Cause:

You're trying to click an element that’s blocked by another element (like a modal or banner).

💡 Fix:

Wait or scroll the element into view.

javaCopyEditWebElement el = driver.findElement(By.id("clickMe"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el);
el.click();

9️⃣ UnhandledAlertException

💣 Cause:

There’s an unexpected alert and you didn’t handle it.

💡 Fix:

Switch to the alert before interacting with anything else.

javaCopyEditAlert alert = driver.switchTo().alert();
alert.accept();

🔟 WebDriverException

💣 Cause:

A generic exception — can happen due to:

  • Browser crash

  • Invalid driver path

  • Session timeout

💡 Fix:

Check the full stack trace and ensure browser-driver compatibility.


🎯 Best Practices

  • Use explicit waits instead of hard sleeps.

  • Wrap risky operations in try-catch blocks.

  • Use a BasePage with common wait and exception handling methods.

  • Regularly update your WebDriver and browser versions.


🧠 Final Thoughts

Mastering Selenium exceptions is part of becoming a smarter automation engineer. The more you understand why things fail, the better your test suites will perform and scale.

0
Subscribe to my newsletter

Read articles from Dinesh Y S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dinesh Y S
Dinesh Y S

Automation Engineer | Java + Selenium | Appium + Java | RestAssured | Sharing real-world automation tips