Exception Handling in Selenium Webdriver (Types)
What is an Exceptions?
An exception is an error that happens at the time of execution of a program. However, while running a program, programming languages generates an exception that should be handled to avoid your program to crash.
The exception indicates that, although the event can occur, this type of event happens infrequently. When the method is not able to handle the Exception, it is thrown to its caller function.
Types of Exceptions in Selenium Webdriver
1. ElementNotVisibleException: This type of Selenium exception occurs when an existing element in DOM has a feature set as hidden.
2. ElementNotSelectableException: This Selenium exception occurs when an element is presented in the DOM, but you can be able to select. Therefore, it is not possible to interact.
3. NoSuchElementException: This Exception occurs if an element could not be found.
4. NoSuchFrameException: This Exception occurs if the frame target to be switched to does not exist.
5. NoAlertPresentException: This Exception occurs when you switch to no presented alert.
6. NoSuchWindowException: This Exception occurs if the window target to be switch does not exist.
7. StaleElementReferenceException: This Selenium exception occurs happens when the web element is detached from the current DOM.
8. SessionNotFoundException: The WebDriver is acting after you quit the browser.
9. TimeoutException: Thrown when there is not enough time for a command to be completed. For Example, the element searched wasn’t found in the specified time.
10. WebDriverException: This Exception takes place when the WebDriver is acting right after you close the browser.
11. ConnectionClosedException: This type of Exception takes place when there is a disconnection in the driver.
12. ElementClickInterceptedException: The command may not be completed as the element receiving the events is concealing the element which was requested clicked.
13. ElementNotInteractableException: This Selenium exception is thrown when any element is presented in the DOM. However, it is impossible to interact with such an element.
14. ErrorInResponseException: This happens while interacting with the Firefox extension or the remote driver server.
15. ErrorHandler.UnknownServerException: Exception is used as a placeholder in case if the server returns an error without a stack trace.
16. ImeActivationFailedException: This expectation will occur when IME engine activation has failed.
17. ImeNotAvailableException: It takes place when IME support is unavailable.
18. InsecureCertificateException: Navigation made the user agent to hit a certificate warning. This can cause by an invalid or expired TLS certificate.
19. InvalidArgumentException: It occurs when an argument does not belong to the expected type.
20. InvalidCookieDomainException: This happens when you try to add a cookie under a different domain instead of current URL.
21. InvalidCoordinatesException: This type of Exception matches an interacting operation that is not valid.
22. InvalidElementStateException: It occurs when command can’t be finished when the element is invalid.
23. InvalidSessionIdException: This Exception took place when the given session ID is not included in the list of active sessions. It means the session does not exist or is inactive either.
24. InvalidSwitchToTargetException: This occurs when the frame or window target to be switched does not exist.
25. JavascriptException: This issue occurs while executing JavaScript given by the user.
26. JsonException: It occurs when you afford to get the session when the session is not created.
27. NoSuchAttributeException: This kind of Exception occurs when the attribute of an element could not be found.
28. MoveTargetOutOfBoundsException: It takes place if the target provided to the ActionChains move() methodology is not valid. For Example, out of the document.
29. NoSuchContextException: ContextAware does mobile device testing.
30. NoSuchCookieException: This Exception occurs when no cookie matching with the given pathname found for all the associated cookies of the currently browsing document.
31. NotFoundException: This Exception is a subclass of WebDriverException. This will occur when an element on the DOM does not exist.
32. RemoteDriverServerException: This Selenium exception is thrown when the server is not responding because of the problem that the capabilities described are not proper.
33. ScreenshotException: It is not possible to capture a screen.
34. SessionNotCreatedException: It happens when a new session could not be successfully created.
35. UnableToSetCookieException: This occurs if a driver is unable to set a cookie.
36. UnexpectedTagNameException: Happens if a support class did not get a web element as expected.
37. UnhandledAlertException: This expectation occurs when there is an alert, but WebDriver is not able to perform Alert operation.
38. UnexpectedAlertPresentException: It occurs when there is the appearance of an unexpected alert.
39. UnknownMethodException: This Exception happens when the requested command matches with a known URL but and not matching with a methodology for a specific URL.
40. UnreachableBrowserException: This Exception occurs only when the browser is not able to be opened or crashed because of some reason.
41. UnsupportedCommandException: This occurs when remote WebDriver doesn’t send valid commands as expected.
How to Handling Exceptions in Selenium
Here, are some important standard using which you can handle Exceptions in Selenium WebDriver:
Step 1) Try-catch
This method can catch Exceptions, which uses a combination of the try and catch keywords. Try command indicates the start of the block, and Catch is placed at the end of the try block, which helps to resolve the Exception.
try { // Code } catch (Exception e) { // Code for Handling exception }
Step 2) Multiple catch blocks
There are various types of Exceptions, and you can expect more than one exception from a single block of code. Multiple catches help you to handle every type of Exception separately with a separate block of code. It can be used for more than two catch blocks, and there is no limitation on the number of catch blocks.
try { //Code } catch (ExceptionType1 e1) { //Code for Handling Exception 1 } catch (ExceptionType2 e2) { //Code for Handling Exception 2 }
Step 3) Throw
When you want to generate an Exception, the Throw keyword is used to throw Exception to handle it in the run time. When you are throwing an Exception without handling it, then they need to use Throw keyword.
public static void anyFunction() throws Exception{ try { // write your code here } Catch (Exception b) { // Do whatever you want to perform // Throw the Exception back to the system throw(b); } }
Step 4) Multiple Exceptions
You can mention various Exceptions in the throws clause.
public static void anyFunction() throws ExceptionType1, ExceptionType2{ try { // write your code here } catch (ExceptionType1 e1) { // Code to handle exception 1 } catch (ExceptionType1 e2) { // Code to handle exception 2 }
Step 5) Finally
The Final keyword is used to create a block of code under the try block. It is the final code that helps you to executes irrespective of the occurrence of an exception
try { //Code } catch (ExceptionType1 e1) { //Catch block } catch (ExceptionType2 e2) { //Catch block } catch (ExceptionType3 e3) { //Catch block } finally { //The finally block always executes. }
Methods for displaying Exception
You can also use the following methods to display Exception Information:
- printStackTrace(): This function prints stack trace, name of the Exception, and other useful description.
- toString(): This function returns a text message describing the exception name and description.
- getMessage(): Helps to displays the description of the Exception.
Summary
- An exception is an error that happens at the time of execution of a program.
- Try-catch: This method can catch Exceptions, which uses a combination of the try and catch keywords.
- Multiple catches help you to handle every type of Exception separately with a separate block of code.
- Throw keyword is used to throw Exception to handle it in the run time.
- printStackTrace(): This function prints stack trace, name of the Exception, and other useful description
- toString(): This function returns a text message describing the exception name and description.
- getMessage(): Helps to displays the description of the Exception.