Selenium WebDriver is a powerful automation tool widely used for web testing and browser automation. As with any software, handling exceptions is a critical aspect of writing robust and reliable automation scripts. In this article, we will explore various exceptions that can occur when using Selenium WebDriver and discuss effective handling techniques to ensure smooth script execution.
Exceptions are basically faults or unwanted unusual event that occurs during execution of program disrupting the flow of execution. Once a exception occurs the program stops execution and the code which is left in behind remains un-executed.
Exceptions may occur dur various number of reasons and since the exceptions break the flow of execution of application it is very much important to handle the exceptions in order to prevent the application from breaking.
It is important to note that exception is different from error . Errors occur due to lack of system resources whereas Exception occurs due to code of the program itself.
Types of Exceptions in Selenium:
Developers an testers are the ones who usually encounter with exceptions and it is important to handle them. Selenium is a very extensively used web automation framework used by testers do cross browser automated testing. During development of automation scripts the testing team should handle multiple exceptions.
Exceptions on a high level can be classified into two major categories i.e. Checked Exceptions and Unchecked Exceptions. Let’s understand about these exceptions better:
- Checked Exceptions: These are types of exceptions which can be figured out during compile time i.e. while writing the code itself. If these type of exception are left unhandled they cause compilation error.
- Unchecked Exceptions: These are types of exceptions which can be figured out during run time and cause more impact than unchecked exceptions.
Common Selenium Exceptions:
Selenium has its own set of exceptions. All runtime exception classes in Selenium WebDriver come under the superclass WebDriverException
- NoSuchWindowException: This kind of exception occurs when web driver is trying to switch to window which does not exist or current list of windows does not exist.
- NoSuchFrameException: Similar to no such window exception, Web Driver is trying to switch to frame which does not exist.
- NoSuchElementException: This type of exception occurs when the web driver is able to identify elements on the web page but unable to find the particular element due to incorrect locaters.
- NoAlertPresentException: Similar to no such window exception, Web Driver is trying to switch to alert which does not exist.
- InvalidSelectorException: This exception occurs due to incorrect selector.
- StaleElementReferenceException: This kind of exception occurs when web element which we are trying to find is no longer part of the web page. This might occur due to many reasons.
- SessionNotFoundException: Web Driver is trying to do some action once the session has been quit.
- WebDriverException:Web Driver is trying to do some action once the session has been closed.
- TimeoutException: This kind of exception occurs when web driver is trying to execute a statement but it got timed out.
- ElementNotSelectableException: This kind of exception occurs when an element is present in the DOM but it is disabled.
Exception Handling Techniques:
Exception handling is a technique by which the above mentioned exceptions can be handled and can be prevent the program from crashing. Below are the list of names of exception handling techniques widely used.
- Using the throw keyword and throwing an explicit exception.
- Using the throws Keyword and mentioning the kind of exceptions that can occur in the method signature and handling them.
- Using single or multiple try, catch and one finally block.
By exploring the various exceptions that can occur in Selenium WebDriver and employing effective handling techniques, developers can ensure the smooth execution of their automation scripts. Understanding the causes behind exceptions and implementing appropriate handling strategies can lead to more reliable and efficient web testing. With the knowledge gained from this article, developers will be well-equipped to tackle exceptions in Selenium WebDriver and create robust automation solutions.