Skip to main content

Command Palette

Search for a command to run...

Selenium Methods with Possible Exceptions (Java)

Published
โ€ข3 min readโ€ขView as Markdown

Selenium Methods with Possible Exceptions (Java)


๐Ÿ”น driver.findElement(By locator)

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
NoSuchElementExceptionElement not found in DOM.
InvalidSelectorExceptionWrong XPath/CSS format.
StaleElementReferenceExceptionElement is no longer attached to DOM.

๐Ÿ”น Handling:

javaCopyEdittry {
    WebElement element = driver.findElement(By.id("username"));
} catch (NoSuchElementException e) {
    System.out.println("Element not found!");
}

๐Ÿ”น element.click()

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
ElementClickInterceptedExceptionElement is blocked by another element (like a modal).
ElementNotInteractableExceptionElement is present but not interactable.
StaleElementReferenceExceptionDOM has changed, element is stale.

๐Ÿ”น Handling:

javaCopyEdittry {
    element.click();
} catch (ElementClickInterceptedException e) {
    System.out.println("Click was blocked!");
}

๐Ÿ”น element.sendKeys("text")

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
InvalidElementStateExceptionElement not editable.
ElementNotInteractableExceptionElement is not interactable.
StaleElementReferenceExceptionElement is detached from DOM.

๐Ÿ”น driver.get("https://url")

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
TimeoutExceptionPage load exceeded time.
WebDriverExceptionBrowser crashed or no connection.
InvalidArgumentExceptionMalformed or empty URL.

๐Ÿ”น driver.switchTo().frame(...)

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
NoSuchFrameExceptionFrame not found.
StaleElementReferenceExceptionFrame element is detached.

๐Ÿ”น driver.switchTo().alert()

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
NoAlertPresentExceptionTried to handle alert when none is present.
UnhandledAlertExceptionAlert is present but not handled properly.

๐Ÿ”น new Select(element)

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
UnexpectedTagNameExceptionElement is not <select>.

๐Ÿ”น driver.navigate().to("url")

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
TimeoutExceptionNavigation took too long.
WebDriverExceptionNavigation failed (e.g., driver not reachable).

๐Ÿ”น driver.close() / driver.quit()

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
NoSuchWindowExceptionWindow already closed.
WebDriverExceptionDriver failed to close properly.

๐Ÿ”น driver.getWindowHandles() / driver.switchTo().window(id)

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
NoSuchWindowExceptionWindow not found.

๐Ÿ”น driver.manage().window().maximize()

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
WebDriverExceptionBrowser doesn't support window resizing (e.g., mobile context).

๐Ÿ”น driver.manage().addCookie(cookie)

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
InvalidCookieDomainExceptionCookie domain mismatch.
UnableToSetCookieExceptionInvalid format or size.

๐Ÿ”น ((TakesScreenshot)driver).getScreenshotAs(...)

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
ScreenshotExceptionCouldnโ€™t take screenshot.
ClassCastExceptionDriver doesn't implement TakesScreenshot.

๐Ÿ”น JavascriptExecutor.executeScript(...)

๐Ÿ”ธ Possible Exceptions:

ExceptionDescription
JavascriptExceptionJS execution error.
UnsupportedOperationExceptionDriver 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;
}

More from this blog

Shreenibas Samal's blog

49 posts