With automation code, it’s pretty common to get an error. An ‘exception,’ as the term indicates, is one-of-a-kind or extraordinary circumstance. Automation code execution may not always run as anticipated due to a number of factors involved in execution, such as network stability problems, Internet difficulties, server stability, and so on. We may get exceptions due to a lack of waiting time, incorrect syntax, parameters, or other issues.
“Exception” is a popular word used by software programmers, regardless of the programming language used to create the code. An “exception,” as the name suggests, is a one-of-a-kind occurrence or condition that disrupts the normal flow of program execution.
Selenium Exceptions can occur for a number of reasons, but they all result in a stop in the program’s execution. When an exception occurs, an exception object is created with debugging information such as the line number, Exception type, and method hierarchy. The process of producing an exception object and delivering it to the runtime environment is referred to as “throwing the exception.”
What does it mean to be an exception?
Exceptions are events that cause a Java program to crash unexpectedly and fail to produce the expected outcomes. Exceptions are handled in Java using a framework. The practice of dealing with exceptions is known as exception handling. Because exceptions disturb the normal flow of a program’s execution, they must be handled.
Common exceptions that you must know
Although Selenium’s documentation has a full list of exceptions, below are a few frequent Selenium exceptions observed while running a test:
I. ElementNotVisibleException:
The element is not visible despite being present in the DOM (can not be interactive). In HTML, for example, elements of the type =”hidden”
II. ElementNotSelectableException:
An element is disabled (cannot be clicked/selected) despite its presence in the DOM.
During runtime, Webdriver is unable to identify elements, i.e. the FindBy function is unable to discover a certain component.
A NoSuchFrameException is thrown when Webdriver tries to switch to an invalid frame that is not available.
Webdriver is attempting to switch to a currently unavailable invalid alert.
Webdriver is attempting to switch to an invalid window that is now unavailable, resulting in a StaleElementReferenceException since the referenced element is no longer present on the DOM page (a reference to a component is now Stale). For example, the item may be from a different frame than the current one, or the user could have navigated to a different website.
After ‘quitting’ the browser, Webdriver responds quickly, resulting in a SessionNotFoundException.
III. TimeoutException:
The command did not complete within the allowed time. For example, the element did not occur at the specified time. This is especially true when working with waiters.
IV. WebDriverException:
Webdriver is still responding after the browser has been ‘closed.’
Conclusion
Every Selenium script has to be able to handle exceptions. Exceptions must be handled because they cause program execution to be disrupted. It is possible to construct optimal and robust programs by controlling Exceptions in unique ways.
Read Also: