Selenium Exceptions:Have you ever met them?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.I am sure we have encountered exceptions in multiple scenarios during our Selenium automation process.
As part of testing many a times we want the test to fail when there is an error.This is true for most of the times.There are some situations where we want to handle errors differently than simply letting it fail.
Without proper error handling the program could come to a complete halt and stop execution of the further important steps.Exception handling become vital in this case so as to save time and ensure the program is executed successfully.
The exception classes are available in selenium.common.exceptions.Selenium exceptions can be classified as checked exceptions and unchecked exceptions.
Checked exceptions are exceptions that are handled during the writing of the code.These are typically handled before compiling the code.
Unchecked exceptions are thrown at runtime.These exceptions are ignored by the complier.
Exception Handling in Selenium Python
This method can catch exceptions, which uses a combination of the try and except keywords. If an error is encountered in the try block the control shifts to the except block and executed.
// Code for Handling exception
Common Exceptions in Selenium WebDriver
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.
Exception handling Scenarios
All the examples shown below requires your code to be inserted wherever appropriate.The examples just address ideas on exception scenarios and how they can be handled.
Common exception encountered when the inspected element is missing.Let us say we are looking at a tweet in twitter to check if that specific tweet has any replies.We may run into a situation where a tweet may not have an error and hence the element is not available.In this example I just append a value of none if I encounter a NoSuchElementException.
This exception is not a very common one but nonetheless very useful to capture timeout errors.The webpage might take more time to load than normal which usually happens when some websites are too slow or they are overloaded.Use this exception with caution as it may end up adding unnecessary execution time if you are testing any stable websites.
I hope this blog has got you started to think about exceptions and how to handle them without compromising on execution time and speed.Hopefully you will never meet an exception!Happy Testing.