Causes, Differences, and Best Practices (2026 Guide)
If you’ve worked with Selenium WebDriver for even a few weeks, you’ve almost certainly encountered NoSuchElementException. It’s one of the most common automation failures, especially when dealing with dynamic web applications. However, if you’ve recently started using Selenium 4’s Relative Locators or Selenium Manager, you may have also noticed ElementNotFoundException. Although both indicate that Selenium couldn’t find an element, they occur in different scenarios and understanding the distinction helps you write more reliable automation frameworks. Here we will understand; NoSuchElementException vs ElementNotFoundException in Selenium.
What is NoSuchElementException?
NoSuchElementException is thrown when Selenium cannot locate an element using the locator you’ve provided.
For example:
driver.findElement(By.id("loginButton"));
If the element does not exist in the DOM, Selenium throws:
org.openqa.selenium.NoSuchElementException:no such element:Unable to locate element
This is the most frequently encountered Selenium exception.
Common Causes of NoSuchElementException
1. Incorrect Locator
Example:
driver.findElement(By.id("submitBtn"));
But actual HTML:
<button id="loginBtn">
The locator is simply wrong.
2. Element Loads Later
Modern applications load elements asynchronously.
Bad:
driver.findElement(By.id("username"));
Better:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated( By.id("username")));
3. Wrong Frame
If the element exists inside an iframe:
driver.switchTo().frame("paymentFrame");
Without switching, Selenium cannot locate the element.
4. Wrong Browser Window
Example:
driver.switchTo().window(windowHandle);
Always verify that Selenium is operating in the correct window or tab.
5. Dynamic IDs
Avoid locators like:
By.id("input_1728391")
Instead prefer:
By.cssSelector("input[name='email']")
or
By.xpath("//input[@placeholder='Email']")
What is ElementNotFoundException?
ElementNotFoundException is a newer Selenium exception used in specific APIs where Selenium attempts to resolve an element based on a relationship or advanced lookup but fails.
It is not the standard exception thrown by a simple findElement() call.
You’ll most commonly encounter it when using features such as:
- Relative Locators
- Certain advanced Selenium APIs
- Internal Selenium operations
Example:
WebElement password =driver.findElement( RelativeLocator.with(By.tagName("input")) .below(username));
If Selenium cannot determine an element below username, it may throw an ElementNotFoundException.
NoSuchElementException vs ElementNotFoundException
| Feature | NoSuchElementException | ElementNotFoundException |
|---|---|---|
| Common? | Very common | Less common |
| Thrown by | findElement() | Advanced Selenium APIs |
| Cause | Locator failed | Relative lookup failed |
| Usually framework issue? | Yes | Sometimes |
| Beginner sees often? | Yes | Rarely |
Example: NoSuchElementException
WebDriver driver = new ChromeDriver();driver.get("https://example.com");driver.findElement(By.id("invalidId"));
Output:
NoSuchElementException
Example Using Relative Locators
WebElement email =driver.findElement(By.id("email"));WebElement password =driver.findElement( RelativeLocator.with(By.tagName("input")) .below(email));
If no matching element exists beneath the email field, Selenium may fail during the relative lookup process.
How to Prevent NoSuchElementException
Use Explicit Waits
Avoid:
driver.findElement(locator);
Prefer:
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
Create Stable Locators
Good:
By.id("email")
Better:
By.cssSelector("[data-testid='email']")
Avoid brittle XPath expressions like:
/html/body/div[2]/div[3]/table/tr[2]/td
Use Page Object Model
Bad:
driver.findElement(By.id("login")).click();
Good:
public class LoginPage { private final By loginButton = By.id("login"); public void clickLogin() { driver.findElement(loginButton).click(); }}
Centralizing locators makes maintenance much easier.
Wait for Clickable Elements
wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
Validate Frames
driver.switchTo().frame("paymentFrame");
Never assume Selenium is already inside the correct frame.
Framework Best Practice: Safe Element Finder
Instead of repeatedly writing:
driver.findElement(locator);
Create a reusable helper.
public class ElementUtil { private WebDriver driver; private WebDriverWait wait; public ElementUtil(WebDriver driver) { this.driver = driver; this.wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } public WebElement find(By locator) { return wait.until( ExpectedConditions.visibilityOfElementLocated(locator)); }}
Usage:
elementUtil.find(By.id("username")).sendKeys("admin");
This approach reduces flaky tests and makes your framework cleaner.
Common Mistakes That Trigger These Exceptions
- Using
Thread.sleep()instead of explicit waits - Relying on dynamic IDs
- Ignoring iframes
- Switching to the wrong browser tab
- Using absolute XPath expressions
- Not waiting for AJAX requests to complete
- Clicking elements before they become visible
- Using stale or outdated locators after UI changes
Best Practices for Large Automation Frameworks
- Use explicit waits instead of implicit waits.
- Prefer CSS Selectors or stable XPath expressions.
- Adopt the Page Object Model (POM).
- Store locators in page classes instead of test classes.
- Use descriptive locator names.
- Create reusable wrapper methods for element interactions.
- Leverage
data-testidor other stable attributes when available. - Capture screenshots and logs when an element lookup fails.
- Integrate detailed reporting with tools like Allure or Extent Reports.
- Review and update locators as part of UI change management.
Conclusion
Although NoSuchElementException and ElementNotFoundException both indicate that Selenium couldn’t resolve an element, they originate from different situations.
NoSuchElementException is the everyday exception you’ll encounter when a locator fails, while ElementNotFoundException is associated with more specialized Selenium operations, such as relative element searches.
By using explicit waits, stable locators, the Page Object Model, and reusable element utilities, you can significantly reduce these failures and build a more maintainable, scalable, and resilient automation framework.
Related Links
✓ Selenium Locators Tutorial
✓ Selenium Waits Explained
✓ Page Object Model in Selenium
✓ Selenium Framework Design
✓ Selenium Grid Explained
✓ Jenkins Pipeline for Selenium Tests
✓ Complete Selenium Tutorial
Discover more from Rotebit
Subscribe to get the latest posts sent to your email.
