Common Errors Cheatsheet
12 most common Selenium errors — what they mean, why they happen, and how to fix them.
Showing 12 of 12 errors
NoSuchElementExceptionWhat it means
The element you are trying to find does not exist in the current DOM.
Common causes
- Wrong locator (typo in ID, incorrect XPath)
- Element has not loaded yet (page still loading)
- Element is inside an iframe (need to switch to frame first)
How to fix
Verify your locator in browser DevTools first. Add an explicit wait before finding the element. Check if the element is inside an iframe.
Example
// Wrong — no wait, element may not be loaded
driver.findElement(By.id("loginBtn")).click();
// Correct — wait for element first
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))).click();StaleElementReferenceExceptionWhat it means
You found an element, but by the time you interact with it, the DOM has changed and the reference is outdated.
Common causes
- Page refreshed or navigated after finding the element
- AJAX call updated the DOM (element was re-rendered)
- Single Page App re-rendered the component
How to fix
Re-find the element after any DOM change. Use explicit waits. In React/Next.js apps, this is common after state updates.
Example
// Wrong — element reference becomes stale after page refresh
WebElement btn = driver.findElement(By.id("submitBtn"));
driver.navigate().refresh();
btn.click(); // StaleElementReferenceException!
// Correct — re-find after DOM change
driver.navigate().refresh();
wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn"))).click();TimeoutExceptionWhat it means
Your explicit wait condition was never met within the timeout period.
Common causes
- Element never appeared on the page
- Wrong ExpectedCondition (e.g., waiting for clickable but element is disabled)
- Timeout too short for the operation
How to fix
Check if the element actually appears on the page. Verify the ExpectedCondition matches what you need. Increase timeout if the page is genuinely slow.
Example
// Check what condition you actually need // If element exists but is hidden: wait.until(ExpectedConditions.presenceOfElementLocated(...)); // not visibility // If element is visible but disabled: wait.until(ExpectedConditions.visibilityOfElementLocated(...)); // not clickable
ElementNotInteractableExceptionWhat it means
Element exists and is found, but you cannot click or type in it right now.
Common causes
- Element is present in DOM but not visible on screen
- Element is behind an overlay, modal, or loading spinner
- Element is disabled or read-only
How to fix
Wait for the element to be visible and clickable. Scroll the element into view. Close any overlays first.
Example
// Scroll into view first
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollIntoView(true)", element
);
// Then wait for clickable
wait.until(ExpectedConditions.elementToBeClickable(element)).click();ElementClickInterceptedExceptionWhat it means
Another element is covering the element you want to click.
Common causes
- Cookie consent banner covering the button
- Floating header/footer overlapping the element
- Modal or popup blocking the click
How to fix
Close the overlay first. Scroll to move the element clear of fixed headers. Use JavaScript click as a last resort.
Example
// Option 1: Close the overlay
driver.findElement(By.id("closeBanner")).click();
// Option 2: Scroll past fixed header
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollIntoView({block: 'center'})", element
);
// Option 3: JavaScript click (bypasses overlay)
((JavascriptExecutor) driver).executeScript("arguments[0].click()", element);InvalidSelectorExceptionWhat it means
Your XPath or CSS selector syntax is wrong.
Common causes
- Typo in XPath expression (missing quotes, wrong brackets)
- Invalid CSS selector syntax
- Using XPath syntax in a CSS selector method or vice versa
How to fix
Test your selector in browser DevTools first: press F12, go to Console, type document.querySelector("your-css") or $x("your-xpath") to verify.
Example
// Wrong — unescaped quotes
driver.findElement(By.xpath("//button[text()="Login"]")); // Syntax error!
// Correct — use single quotes inside double quotes
driver.findElement(By.xpath("//button[text()='Login']"));
// Test in DevTools Console first:
// $x("//button[text()='Login']") → for XPath
// document.querySelector("#loginBtn") → for CSSWebDriverExceptionWhat it means
Generic driver-level failure — the browser or driver crashed.
Common causes
- ChromeDriver version does not match Chrome browser version
- Browser crashed or was closed externally
- Driver executable not found in PATH
How to fix
Use WebDriverManager to auto-manage driver versions. Check browser is installed and not being updated during tests.
Example
// Use WebDriverManager to avoid version mismatch WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); // Or in Selenium 4.6+ (built-in Selenium Manager): WebDriver driver = new ChromeDriver(); // Auto-downloads driver
SessionNotCreatedExceptionWhat it means
Cannot start a new browser session.
Common causes
- Chrome browser version incompatible with ChromeDriver version
- Browser not installed on the machine
- Another process using the same debug port
How to fix
Update ChromeDriver to match your Chrome version. Use WebDriverManager for automatic version management.
Example
// Check Chrome version: chrome://version // Check ChromeDriver version: chromedriver --version // They must match (e.g., Chrome 124 → ChromeDriver 124) // Best fix — use WebDriverManager: WebDriverManager.chromedriver().setup();
NoAlertPresentExceptionWhat it means
You tried to switch to a JavaScript alert, but no alert is displayed.
Common causes
- Alert has not appeared yet (need to wait)
- Alert was already accepted/dismissed
- The dialog is a custom HTML modal, not a browser alert
How to fix
Wait for the alert to appear before interacting. Check if it is a real browser alert or a custom HTML modal (custom modals need regular locators, not switchTo().alert()).
Example
// Wait for alert first
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
// If it's a custom modal (not a browser alert):
driver.findElement(By.id("confirmBtn")).click(); // Use regular locatorMoveTargetOutOfBoundsExceptionWhat it means
The Actions class tried to move to a point outside the browser viewport.
Common causes
- Element is off-screen (scrolled out of view)
- Element has zero width or height
- Using wrong coordinates for moveByOffset()
How to fix
Scroll the element into view before performing Actions on it.
Example
// Scroll into view first
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollIntoView({block: 'center'})", element
);
// Then perform the action
new Actions(driver).moveToElement(element).click().perform();JavascriptExceptionWhat it means
The JavaScript code you executed via JavascriptExecutor threw an error.
Common causes
- Syntax error in your JavaScript code string
- Trying to access a null/undefined variable in JS
- Element reference (arguments[0]) is null
How to fix
Test your JavaScript in the browser DevTools Console first. Make sure the element reference is valid.
Example
// Test JS in DevTools Console first:
// document.querySelector("#loginBtn").scrollIntoView()
// Wrong — element might be null
((JavascriptExecutor) driver).executeScript(
"arguments[0].click()", null // throws JavascriptException
);
// Correct — find element first, verify it exists
WebElement element = driver.findElement(By.id("loginBtn"));
((JavascriptExecutor) driver).executeScript("arguments[0].click()", element);UnhandledAlertExceptionWhat it means
An unexpected browser alert appeared while trying to perform another action.
Common causes
- Previous action triggered an alert you did not expect
- Server returned an error that showed as an alert
- Confirmation dialog appeared before navigation
How to fix
Handle the alert before continuing. Add a try-catch to dismiss unexpected alerts.
Example
// Handle unexpected alerts
try {
driver.findElement(By.id("nextBtn")).click();
} catch (UnhandledAlertException e) {
driver.switchTo().alert().accept(); // Dismiss the alert
driver.findElement(By.id("nextBtn")).click(); // Retry
}