To many programmers, there is no strangeness when they hear the term “exception”. To testers, in particular, this is inevitable. Exceptions are scenarios that occur during the execution of a program that disrupts the normal flow and creates exception objects. Nevertheless, achieving them is not too difficult or impossible. We will discuss these Common Exceptions in Selenium subsequently.
Therefore, 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. We will cover the below topics in this article:-
- What are Selenium Exceptions?
- Classification of Selenium Exceptions
- What are the common exceptions in Selenium WebDriver?
What are Selenium Exceptions?
An exception is known as an unusual or unprecedented event that occurs during the execution of a software program/ application. It is a runtime error of an unexpected result or event which influences and disrupts usual program flow. We also consider an exception as a fault.
Classification of Selenium Exceptions
We can divide the Selenium exceptions into two types, including Checked Exceptions and Unchecked Exceptions.
Checked Exceptions
We can handle the Checked Exceptions during the process of writing codes. We handle these exceptions before compiling the code. Therefore, we examine such exceptions at the compile time.
Unchecked Exceptions
These exceptions throw at runtime. Unchecked exceptions are more disastrous than the compile-time exception as it causes problems while running Automation pack in headless.
UPDATED: Katalon Studio currently supports executing test cases from Selenium, TestNG, or JUnit projects. You can easily migrate them to a destination Katalon Studio project. Migrating test scripts from Selenium and TestNG/JUnit to Katalon Studio help users to eliminate the need to start everything from scratch. We can reuse the Test cases after the migration, and you can enjoy Katalon features to execute and maintain them.
Common Exceptions in Selenium WebDriver
Here is a list of some of the most popular exceptions in Selenium WebDriver, which you may or may not encounter.
- TimeoutException: Thrown when there is not enough time for a command to complete.
- 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.
- 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 finish as the element is invalid.
- 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.
- InvalidSwitchToTargetException: Happens if a frame or window target to switch does not exist.
- ElementClickInterceptedException: The command could not complete as the element receiving the events is concealing the element which was requested clicked.
- ElementNotInteractableException: This Selenium exception gets thrown when an element is present in the DOM, but it is impossible to interact with such an element.
- NoAlertPresentException: Happens when you switch to no presented alert.
- NoSuchAttributeException: This occurs when we can’t find the attribute of the element.
- NoSuchContextException: Happens in mobile device testing and is thrown by ContextAware.
- 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.
- NoSuchElementException: Happens if we can't find an element.
- NoSuchFrameException: Takes place if frame target to be switch does not exist.
- NoSuchWindowException: This occurs if the window target to be switch does not exist.
- NotFoundException: This exception is a subclass of WebDriverException. It happens when an element on the DOM doesn't exist.
Conclusion
Exceptions are unavoidable during the execution of any test automation projects. By explaining the most famous exceptions that we have caught in Selenium, we want to create 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.