0% found this document useful (0 votes)
19 views

Exceptions in Selenium

Uploaded by

arulmani222
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Exceptions in Selenium

Uploaded by

arulmani222
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MOST IMPORTANT SELENIUM EXCEPTIONS

1. NoSuchElementException

This exception occurs when the element you are trying to locate is not found in the
DOM.

How to handle:

- Double-check the locator to make sure it is correct.

- 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:

- Increase the timeout duration for waiting on the element.

- 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.

- Ensure that no other element is overlapping or hiding the element.

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.

- Implement a retry mechanism in your test script.

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:

- Restart the WebDriver and try the operation again.

- Ensure the WebDriver version matches the browser version.

PDF prepared by Ram Sharan Follow me on Linkedin


MOST IMPORTANT SELENIUM EXCEPTIONS

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.

- Make sure the syntax of the locator is correct.

7. NoSuchWindowException

This occurs when WebDriver tries to switch to a window that does not exist or has been
closed.

How to handle:

- Ensure the target window is open before switching.

- 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.

- Use an explicit wait for the frame to be available.

9. ElementNotInteractableException

This occurs when the element is present but cannot be interacted with (e.g., disabled or
hidden).

How to handle:

- Wait until the element is clickable or interactable before performing an action.

- Check if the element is disabled or if its properties change dynamically.

PDF prepared by Ram Sharan Follow me on Linkedin


MOST IMPORTANT SELENIUM EXCEPTIONS

10. SessionNotFoundException

This occurs when the WebDriver session is no longer valid, usually after a browser or
WebDriver crash.

How to handle:

- Reinitialize the WebDriver session and restart the test case.

- Ensure proper session handling to avoid session timeouts or closures.

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:

- Scroll the element into view before interacting with it.

- Make sure the element's coordinates are valid and within the visible screen area.

12. InvalidArgumentException

This occurs when an invalid argument is passed to a WebDriver command


(e.g., an invalid URL format or incorrect input data).

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.

PDF prepared by Ram Sharan Follow me on Linkedin


MOST IMPORTANT SELENIUM EXCEPTIONS

14. InvalidCookieDomainException

This occurs when attempting to add a cookie under a different domain than the current
one.

How to handle:

- Ensure the cookie being added is for the correct domain.

- Set cookies only for the currently loaded domain.

15. NoAlertPresentException

This occurs when WebDriver tries to interact with an alert that is not present.

How to handle:

- Check if the alert is present before trying to interact with it.

- 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.

- Ensure no overlapping element is covering the target element.

17. UnhandledAlertException

This exception occurs when there is an unhandled alert present, and WebDriver is
trying to perform an action.

How to handle:

- Dismiss or accept the alert before continuing with other actions.

- Use an explicit wait for the alert to appear and handle it properly

PDF prepared by Ram Sharan Follow me on Linkedin


MOST IMPORTANT SELENIUM EXCEPTIONS

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:

- Reinitialize the WebDriver session.

- Ensure proper session handling and avoid unintentional session terminations.

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.

- Scroll the element into view if needed.

PDF prepared by Ram Sharan Follow me on Linkedin


MOST IMPORTANT SELENIUM EXCEPTIONS

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.

- Double-check the documentation for alternative commands or methods.

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.

- If not using IME, avoid relying on this feature in the test.

24. ImeActivationFailedException

This occurs when WebDriver fails to activate the IME.

How to handle:

- Ensure that the correct IME is selected and available for activation.

- Check for system or configuration issues preventing IME 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.

PDF prepared by Ram Sharan Follow me on Linkedin

You might also like