top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Selenium Exceptions:Have you ever met them?

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.I am sure we have encountered exceptions in multiple scenarios during our Selenium automation process.


As part of testing many a times we want the test to fail when there is an error.This is true for most of the times.There are some situations where we want to handle errors differently than simply letting it fail.


Without proper error handling the program could come to a complete halt and stop execution of the further important steps.Exception handling become vital in this case so as to save time and ensure the program is executed successfully.


The exception classes are available in selenium.common.exceptions.Selenium exceptions can be classified as checked exceptions and unchecked exceptions.


Checked exceptions are exceptions that are handled during the writing of the code.These are typically handled before compiling the code.


Unchecked exceptions are thrown at runtime.These exceptions are ignored by the complier.


Exception Handling in Selenium Python


Try-except:


This method can catch exceptions, which uses a combination of the try and except keywords. If an error is encountered in the try block the control shifts to the except block and executed.


try:


// Code


except exceptionType:


// Code for Handling exception


Common Exceptions in Selenium WebDriver

  1. TimeoutException: Thrown when there is not enough time for a command to complete.

  2. UnableToCreateProfileException: You can open a browser with specific options using profiles, but sometimes a new version of Selenium Driverserver or browser may not support the profiles.

  3. UnableToSetCookieException: This occurs if a driver is unable to set a cookie.

  4. UnexpectedAlertPresentException: This Selenium exception happens when there is the appearance of an unexpected alert.

  5. UnexpectedTagNameException: Happens if a support class did not get a web element as expected.

  6. InvalidCookieDomainException: This happens when you try to add a cookie under a different domain rather than the current URL.

  7. InvalidCoordinatesException: This happens if the coordinates offered to an interacting operation are not valid.

  8. InvalidElementStateException: This Selenium exception occurs if a command cannot finish as the element is invalid.

  9. InvalidSessionIdException: Takes place when the given session ID does not include in the list of active sessions, which means the session does not exist or is inactive either.

  10. InvalidSwitchToTargetException: Happens if a frame or window target to switch does not exist.

  11. ElementClickInterceptedException: The command could not complete as the element receiving the events is concealing the element which was requested clicked.

  12. ElementNotInteractableException: This Selenium exception gets thrown when an element is present in the DOM, but it is impossible to interact with such an element.

  13. NoAlertPresentException: Happens when you switch to no presented alert.

  14. NoSuchAttributeException: This occurs when we can’t find the attribute of the element.

  15. NoSuchContextException: Happens in mobile device testing and is thrown by ContextAware.

  16. NoSuchCookieException: This exception gets thrown if there is no cookie matching with the given pathname found amongst the associated cookies of the current browsing context’s active document.

  17. NoSuchElementException: Happens if we can't find an element.

  18. NoSuchFrameException: Takes place if frame target to be switch does not exist.

  19. NoSuchWindowException: This occurs if the window target to be switch does not exist.

  20. NotFoundException: This exception is a subclass of WebDriverException. It happens when an element on the DOM doesn't exist.


Exception handling Scenarios


All the examples shown below requires your code to be inserted wherever appropriate.The examples just address ideas on exception scenarios and how they can be handled.


1.NoSuchElementException


Common exception encountered when the inspected element is missing.Let us say we are looking at a tweet in twitter to check if that specific tweet has any replies.We may run into a situation where a tweet may not have an error and hence the element is not available.In this example I just append a value of none if I encounter a NoSuchElementException.




2.TimeoutException


This exception is not a very common one but nonetheless very useful to capture timeout errors.The webpage might take more time to load than normal which usually happens when some websites are too slow or they are overloaded.Use this exception with caution as it may end up adding unnecessary execution time if you are testing any stable websites.




3. ElementClickInterceptedException


This exception is thrown when the element you try to click an element but it gets blocked by another element and becomes unclickable.The common cause most of the time is due to a pop-up being triggered or some actions that require interaction.This can be handled by simply closing the pop up or through a javascript executor.



I hope this blog has got you started to think about exceptions and how to handle them without compromising on execution time and speed.Hopefully you will never meet an exception!Happy Testing.

514 views0 comments

Recent Posts

See All
bottom of page