top of page
Writer's picturesreelatha yennam

Exception Handling in Selenium Webdriver


What is an exception?

An exception is an error that occurs during the execution of a program. However, while running a program, programming languages generate an exception that must be handled to prevent the program from crashing. The exception indicates that, although the event can occur, this type of event happens infrequently. When a method is unable to handle the exception, it is thrown to its caller function.


Types of Exceptions


1. ElementNotVisibleException: This occurs when an element exists in the DOM but is hidden from view.

   Solution: Make sure the element is visible before interacting with it.


2. ElementNotSelectableException: This exception arises when an element is in the DOM but cannot be selected.

   - Solution: Confirm that the element can be interacted with or selected before performing actions on it.


3. NoSuchElementException: When an element cannot be found in the DOM, this exception is thrown.

   - Solution: Double-check the locator strategy used to find the element and ensure it exists before interacting with it.


4. NoSuchFrameException: This exception happens if the targeted frame to switch to does not exist.

   - Solution: Verify the frame identifier or index used for switching frames.


5. NoAlertPresentException: Occurs when attempting to switch to a non-presented alert.

   - Solution: Handle with a try-catch block or verify alert presence before interacting with it.


6. NoSuchWindowException: Thrown if the window to switch to does not exist.

   - Solution: Ensure the correct window handles are used or verify the window context.


7. StaleElementReferenceException: This exception occurs when a web element is no longer attached to the DOM.

   - Solution: Re-locate the element or refresh the page before interacting with it.


8. SessionNotFoundException: Occurs when WebDriver actions are attempted after quitting the browser.

   - Solution: Manage WebDriver sessions properly to avoid actions after quitting the browser.


9. TimeoutException: Thrown when a command cannot be completed within a specified time.

   - Solution: Increase the timeout duration or handle the operation asynchronously.


10. WebDriverException: A generic exception for WebDriver-related issues.

    - Solution: Handle specific exceptions that extend WebDriverException according to their specific causes.


11. ConnectionClosedException: This occurs when there is a disconnection in the WebDriver.

    - Solution: Restart the WebDriver session or resolve network issues causing the disconnection.


12. ElementClickInterceptedException: Happens when another element obstructs the element being clicked.

    - Solution: Use the Actions class to perform the click action or move to the element before interacting with it.


13. ElementNotInteractableException: Thrown when an element is present but cannot be interacted with.

    - Solution: Ensure the element is in a valid state (enabled and visible) before attempting interactions.


14. ErrorInResponseException: Occurs during interaction with Firefox extensions or remote driver server.

    - Solution: Check and adjust server configurations and interaction methods as needed.


15. ErrorHandler.UnknownServerException: Placeholder for server errors without a stack trace.

    - Solution: Investigate server logs and configurations to resolve underlying server-side issues.


16. ImeActivationFailedException: Happens when IME engine activation fails.

    - Solution: Verify and configure IME settings correctly for successful activation.


17. ImeNotAvailableException: Thrown when IME support is unavailable.

    - Solution: Enable IME support or handle cases where IME input is not required.


18. InsecureCertificateException: Occurs when navigation triggers a certificate warning.

    - Solution: Handle certificate warnings by accepting insecure certificates or configuring valid ones.


19. InvalidArgumentException: Thrown when an argument does not match the expected type.

    - Solution: Correct the argument type to match the expected type for the method or operation.


20. InvalidCookieDomainException: This exception occurs when trying to add a cookie under a different domain.

    - Solution: Set cookies under the current domain to avoid domain mismatch issues.


21. InvalidCoordinatesException: Occurs when an invalid coordinate is provided for an interaction.

    - Solution: Provide valid coordinates within the document bounds for the interaction.


22. InvalidElementStateException: Thrown when a command cannot be executed on an invalid element state.

    - Solution: Ensure the element state is valid (such as being visible and enabled) for the command being executed.


23. InvalidSessionIdException: Happens when the provided session ID is not active or does not exist.

    - Solution: Verify and provide a valid session ID that is active and exists in the session management.


24. InvalidSwitchToTargetException: Occurs when attempting to switch to an invalid frame or window target.

    - Solution: Verify the target frame or window exists and is correctly specified for switching.


25. JavascriptException: This occurs when executing user-provided JavaScript encounters an issue.

    - Solution: Check the JavaScript code for syntax errors and ensure it executes correctly in the current context.


26. JsonException: Thrown when attempting to access a session that does not exist or is not created.

    - Solution: Ensure proper session management and verify session creation before accessing it.


27. NoSuchAttributeException: Occurs when an element attribute could not be found.

    - Solution: Verify the attribute name and existence before attempting to access it.


28. MoveTargetOutOfBoundsException: Happens when the target of an Actions class move() method is out of bounds.

    - Solution: Provide a valid target within the document bounds for the move() method to avoid this exception.


29. NoSuchContextException: Occurs in mobile device testing when the context is not found.

    - Solution: Verify and handle mobile context correctly or switch to the correct context if available.


30. NoSuchCookieException: Thrown when no cookie matching the pathname is found.

    - Solution: Verify the cookie pathname or handle the absence of specific cookies gracefully.


31. NotFoundException: Subclass of WebDriverException, occurs when an element does not exist in the DOM.

    - Solution: Verify the existence of the element in the DOM before attempting to interact with it.


32. RemoteDriverServerException: Thrown when server capabilities are not properly configured or responded.

    - Solution: Verify server capabilities and configuration to ensure proper communication and response handling.


33. ScreenshotException: Occurs when capturing a screenshot encounters an issue.

    - Solution: Check file system permissions or handle issues related to capturing screenshots effectively.


34. SessionNotCreatedException: Happens when a new WebDriver session could not be successfully created.

    - Solution: Verify WebDriver configuration and capabilities to ensure sessions can be created successfully.


35. UnableToSetCookieException: Thrown when WebDriver is unable to set a cookie.

    - Solution: Ensure cookie properties are correctly set and the browser state allows for setting cookies.


36. UnexpectedTagNameException: Occurs when an element is not of the expected tag name.

    - Solution: Verify the element type or tag name before attempting to interact with or access it.


37. UnhandledAlertException: Happens when WebDriver encounters an alert but cannot handle it.

    - Solution: Use the switchTo().alert() method to explicitly handle alerts when encountered.


38. UnexpectedAlertPresentException: Occurs when an unexpected alert is present.

    - Solution: Handle the unexpected alert by dismissing, accepting, or handling it according to application logic.


39. UnknownMethodException: Thrown when WebDriver receives a command that does not match any known URL.

    - Solution: Ensure WebDriver commands sent are valid and supported by the WebDriver instance and version used.


40. UnreachableBrowserException: Occurs when the browser cannot be opened or crashes unexpectedly.

    - Solution: Restart the browser or handle system/browser issues causing the browser to be unreachable.


41. UnsupportedCommandException: Thrown when remote WebDriver does not support the command sent.

    - Solution: Verify WebDriver capabilities and command compatibility with the remote WebDriver instance being used.


31 views

Recent Posts

See All
bottom of page