Selenium Methods with Possible Exceptions (Java)
๐น driver.findElement(By locator)
๐ธ Possible Exceptions:
| Exception | Description |
NoSuchElementException | Element not found in DOM. |
InvalidSelectorException | Wrong XPath/CSS format. |
StaleElementReferenceException | Element is no longer attached to DOM. |
๐น Handling:
javaCopyEdittry {
WebElement element = driver.findElement(By.id("username"));
} catch (NoSuchElementException e) {
System.out.println("Element not found!");
}
๐ธ Possible Exceptions:
| Exception | Description |
ElementClickInterceptedException | Element is blocked by another element (like a modal). |
ElementNotInteractableException | Element is present but not interactable. |
StaleElementReferenceException | DOM has changed, element is stale. |
๐น Handling:
javaCopyEdittry {
element.click();
} catch (ElementClickInterceptedException e) {
System.out.println("Click was blocked!");
}
๐น element.sendKeys("text")
๐ธ Possible Exceptions:
| Exception | Description |
InvalidElementStateException | Element not editable. |
ElementNotInteractableException | Element is not interactable. |
StaleElementReferenceException | Element is detached from DOM. |
๐ธ Possible Exceptions:
| Exception | Description |
TimeoutException | Page load exceeded time. |
WebDriverException | Browser crashed or no connection. |
InvalidArgumentException | Malformed or empty URL. |
๐น driver.switchTo().frame(...)
๐ธ Possible Exceptions:
| Exception | Description |
NoSuchFrameException | Frame not found. |
StaleElementReferenceException | Frame element is detached. |
๐น driver.switchTo().alert()
๐ธ Possible Exceptions:
| Exception | Description |
NoAlertPresentException | Tried to handle alert when none is present. |
UnhandledAlertException | Alert is present but not handled properly. |
๐น new Select(element)
๐ธ Possible Exceptions:
| Exception | Description |
UnexpectedTagNameException | Element is not <select>. |
๐น driver.navigate().to("url")
๐ธ Possible Exceptions:
| Exception | Description |
TimeoutException | Navigation took too long. |
WebDriverException | Navigation failed (e.g., driver not reachable). |
๐น driver.close() / driver.quit()
๐ธ Possible Exceptions:
| Exception | Description |
NoSuchWindowException | Window already closed. |
WebDriverException | Driver failed to close properly. |
๐น driver.getWindowHandles() / driver.switchTo().window(id)
๐ธ Possible Exceptions:
| Exception | Description |
NoSuchWindowException | Window not found. |
๐น driver.manage().window().maximize()
๐ธ Possible Exceptions:
| Exception | Description |
WebDriverException | Browser doesn't support window resizing (e.g., mobile context). |
๐น driver.manage().addCookie(cookie)
๐ธ Possible Exceptions:
| Exception | Description |
InvalidCookieDomainException | Cookie domain mismatch. |
UnableToSetCookieException | Invalid format or size. |
๐น ((TakesScreenshot)driver).getScreenshotAs(...)
๐ธ Possible Exceptions:
| Exception | Description |
ScreenshotException | Couldnโt take screenshot. |
ClassCastException | Driver doesn't implement TakesScreenshot. |
๐น JavascriptExecutor.executeScript(...)
๐ธ Possible Exceptions:
| Exception | Description |
JavascriptException | JS execution error. |
UnsupportedOperationException | Driver doesnโt support JS execution. |
๐ ๏ธ Best Practice: General Exception Handling Wrapper
You can create a generic wrapper to handle exceptions across your framework:
javaCopyEditpublic WebElement safeFindElement(By locator) {
try {
return driver.findElement(locator);
} catch (NoSuchElementException e) {
System.out.println("Element not found: " + locator);
} catch (InvalidSelectorException e) {
System.out.println("Invalid selector: " + locator);
}
return null;
}