Exceptions in Selenium
Exceptions in Selenium
1. NoSuchElementException
This exception occurs when the element you are trying to locate is not found in the
DOM.
How to handle:
- Use an explicit wait to ensure that the element is available before interacting with it.
2. TimeoutException
This occurs when a command takes longer than the specified wait time.
How to handle:
- Ensure the element is loaded and visible within the allowed time.
3. ElementNotVisibleException
This occurs when an element is present in the DOM but not visible on the screen.
How to handle:
- Wait for the element to become visible before interacting with it.
4. StaleElementReferenceException
This occurs when the referenced element is no longer attached to the DOM, usually after
a page reload.
How to handle:
- Refetch the element after the page has refreshed or the DOM has been updated.
5. WebDriverException
A general exception that occurs when WebDriver cannot perform an action due to an
unexpected issue (e.g., browser crash or driver miscommunication).
How to handle:
6. InvalidSelectorException
This exception occurs when the provided selector (XPath or CSS) is not valid.
How to handle:
- Verify that the locator is correctly formatted and points to an existing element.
7. NoSuchWindowException
This occurs when WebDriver tries to switch to a window that does not exist or has been
closed.
How to handle:
- Retrieve the current list of open window handles before attempting to switch.
8. NoSuchFrameException
This occurs when WebDriver tries to switch to a frame that is not available.
How to handle:
- Make sure the frame is present and correct before switching to it.
9. ElementNotInteractableException
This occurs when the element is present but cannot be interacted with (e.g., disabled or
hidden).
How to handle:
10. SessionNotFoundException
This occurs when the WebDriver session is no longer valid, usually after a browser or
WebDriver crash.
How to handle:
11. MoveTargetOutOfBoundsException
This occurs when WebDriver tries to move the mouse to an element that is outside the
viewport or not within the browser window's current view.
How to handle:
- Make sure the element's coordinates are valid and within the visible screen area.
12. InvalidArgumentException
How to handle:
- Ensure that the arguments passed are correct, such as valid URLs, selectors, or input
data.
- Double-check the types of the arguments and correct any formatting issues.
13. JavascriptException
This exception occurs when there is an issue with executing JavaScript through
WebDriver.
How to handle:
- Ensure the JavaScript you are trying to execute is correct and error-free.
- Make sure the element targeted by the script is loaded and interactable.
14. InvalidCookieDomainException
This occurs when attempting to add a cookie under a different domain than the current
one.
How to handle:
15. NoAlertPresentException
This occurs when WebDriver tries to interact with an alert that is not present.
How to handle:
- Use an explicit wait to wait for the alert to appear before proceeding.
16. ElementClickInterceptedException
This occurs when an element is being obscured by another element, preventing a click
action.
How to handle:
- Scroll the element into view or use JavaScript to click on the element.
17. UnhandledAlertException
This exception occurs when there is an unhandled alert present, and WebDriver is
trying to perform an action.
How to handle:
- Use an explicit wait for the alert to appear and handle it properly
18. NoSuchSessionException
This occurs when a WebDriver session is no longer active, typically due to the browser
being closed or crashed unexpectedly.
How to handle:
Here are a few more exceptions in Selenium that you might encounter, along with how
to handle them:
19. UnableToSetCookieException
This exception occurs when WebDriver is unable to set a cookie in the browser.
How to handle:
- Ensure that the cookie is being set for the correct domain.
- Check that the cookie format is valid and the browser settings allow cookies.
20. UnexpectedTagNameException
This occurs when a WebDriver command is performed on an element, but the element's
tag name does not match the expected tag name
(for example, trying to interact with a `select` element, but the actual element is not a
`select`).
How to handle:
- Ensure the correct element is being targeted with the proper tag name.
- Double-check the locator to ensure you are interacting with the intended element.
21. InvalidCoordinatesException
This occurs when WebDriver attempts to interact with coordinates outside the bounds
of the screen or an element.
How to handle:
- Ensure the coordinates you are using are valid and within the screen or element's
boundaries.
22. UnsupportedCommandException
This occurs when WebDriver encounters a command that is not supported by the
browser.
How to handle:
- Ensure the WebDriver version and the browser support the command you are using.
23. ImeNotAvailableException
This occurs when an Input Method Editor (IME) is not available on the system or
browser for handling input methods (for non-Latin languages).
How to handle:
- Ensure that the IME is installed and available on the system where the browser is
running.
24. ImeActivationFailedException
How to handle:
- Ensure that the correct IME is selected and available for activation.
25. NotFoundException
This is a generic exception that occurs when an element or object is not found
(could be an element, window, frame, etc.).
How to handle:
- Ensure the locator is correct and that the object exists in the DOM.
- Use appropriate waits to ensure the object is available before interacting with it.