To many programmers, there is no strangeness when they hear the term “exception”. To testers, in particular, this is inevitable. Basically, exceptions are scenarios occur during the execution of a program that disrupts the normal flow and creates exception objects. Nevertheless, achieving them is not something that is too difficult or impossible.
Thus, here in this article, let’s have a look at Common Exceptions in Selenium WebDriver and their brief explanations one by one so that you could find the most suitable ways to handle them.
What are Selenium exceptions?
An exception is known as an unusual or unprecedented event that occurs during the execution of a software program or application. It is a runtime error of an unexpected result or event which influences and disrupts usual program flow. An exception is also considered as a fault.
Classification of Selenium exceptions
Selenium exceptions are divided into two types including Checked Exceptions and Unchecked Exceptions.
Checked Exceptions are handled during the process of writing codes. These exceptions are handled before compiling the code, therefore, such exceptions are examined at the compile time.
These exceptions are thrown at runtime. Unchecked exceptions are more catastrophic than the compile-time exception as it causes problems while running Automation pack in headless.
Common Exceptions in Selenium WebDriver
Here is a list of some of the most well-known exceptions in Selenium WebDriver which you may or may not encounter.
- TimeoutException: Thrown when there is not enough time for a command to be completed.
- UnableToCreateProfileException: You can open a browser with certain options using profiles, but sometimes a new version of Selenium Driverserver or browser may not support the profiles.
- UnableToSetCookieException: This occurs if a driver is unable to set a cookie.
- UnexpectedAlertPresentException: This Selenium exception happens when there is the appearance of an unexpected alert.
- UnexpectedTagNameException: Happens if a support class did not get a web element as expected.
- InvalidCookieDomainException: This happens when you try to add a cookie under a different domain rather than the current URL.
- InvalidCoordinatesException: This happens if the coordinates offered to an interacting operation are not valid.
- InvalidElementStateException: This Selenium exception occurs if a command cannot be finished as the element is invalid.
- InvalidSessionIdException: Takes place when the given session ID is not included in the list of active sessions, which means the session does not exist or is inactive either.
- InvalidSwitchToTargetException: Happens if frame or window target to be switched does not exist.
- ElementClickInterceptedException: The command could not be completed as the element receiving the events is concealing the element which was requested clicked.
- ElementNotInteractableException: This Selenium exception is thrown when an element is presented in the DOM but it is impossible to interact with such element.
- NoAlertPresentException: Happens when you switch to no presented alert.
- NoSuchAttributeException: This occurs when the attribute of the element could not be found.
- NoSuchContextException: Happens in mobile device testing and is thrown by ContextAware.
- NoSuchCookieException: This exception is thrown if there is no cookie matching with the given pathname found amongst the associated cookies of the current browsing context’s active document.
- NoSuchElementException: Happens if an element could not be found.
- NoSuchFrameException: Takes place if frame target to be switch does not exist.
- NoSuchWindowException: Occurs if window target to be switch does not exist.
- NotFoundException: This exception is subclass of WebDriverException. It happens when an element on the DOM does not exist.
Exceptions are unavoidable during the execution of any test automation projects. By explaining the most popular exceptions that we have caught in Selenium, we want to create a quick documentation for you to make your tests more resilient. Hopefully, this could also help you learn how to work effectively with exceptions in other testing tools.