What is an Exception
An exception is a problem that occurs during the execution of a program. It will interrupt the regular flow of a program. An exception object is created when an exception is encountered, which contains a lot of debugging information such as the line number where the exception occurred , the type of Exception, and the method hierarchy and etc. After an exception object is created and passed to the runtime, this process is called 'throwing the exception'. Once it throw an Exception we must have remove those is called Exception handling
Types of Exceptions
There are two types of Exceptions
Checked Exceptions:
These Exceptions occurs at compile time .Most of the IDE's will highlight these exceptions while writing code we can handle them before compilation. These exceptions can be handled by the try-catch block or by using throws keyword.
Unchecked Exceptions:
These exceptions which occur during the runtime. IDE's will not highlight these exceptions. We have to analyse and debug them.
Once Exception occured we have to handle there is two ways we can handle
syntax:
try{
//statements that may raise exception
}catch(exception e){
// Display message when exception occurs
}finally{
}
finally block is not mandatory finally block will execute in both cases(exception, not exception).
Below are the most common exceptions in selenium
Invalid Argument Exception
When we passed invalid argument to get command will get this Invalid Argument Exception.
Above code URL is invalid (https is not included)
InvalidSelectorException
When we pass invalid synatax of Xpath or CSS selector will get this Exception
solution: we have to correct the syntax
No Such Element Exception
There is small difference between Invalid Selector Exception and No such Element Exception
If we gave wrong attribute and value it will through No Such Element Exception
solution: we have correct the attribute names and its values
No Such Window Exception
This occurs when we are trying to switch to a window which does not exist
No Alert Present Exception
This occurs when we are trying to switch to an alert which is not displayed on the page
No Such Frame Exception
This occurs when we are trying to switch to an I frame which does not exist
Element Click Intercepted Exception
This exception occurs when other UI elements hides the UI element to be clicked during the Automation
refer above image click on the blogs first then I'm trying to access the link under that 'this blog is created link' so it is throwing Element Click Intercepted Exception
Element Not Interactable Exception
This exception occur when element is present in the DOM but it is impossible to interact with such element.
example:
* When we are trying to click on the button which is available in the DOM but it is not visible in the application(hidden element)
* We are trying enter text in element which is not accept the text like form tag, heading tag and label tag
In above code I tried to send text in to label tag which is present in DOM but it won't accept the text
Java script Exception
We get this exception when we pass JavaScript statements with syntax problems in an automation script.
above code single quotes missed
Un Expected Tag Name
Methods like selectByValue(), selectByIndex(), or selectByVisibleText() are intended to be used with select elements. If these methods are used on a different type of element, like div, span, or input, an Un Expected Tag Name Exception will be thrown.
Below is an example of using the ‘Button’ element in the Select class:
Unhandled Alert Exception
this exception occurs when there is an alert, driver is trying to perform action before Alert handling the alert(dismiss or accept)
Illegal State Exception
If the chromedriver.exe file path is incorrectly specified in the System. set Property method, an Illegal State Exception will be thrown when you run the Chrome browser.
Stale Element Reference Exception
When we try to use a web Element after navigating away from the page and coming back to the page where the Web Element present
Page Load Timeout Exception
By default, the driver waits until the page is loaded, so there is no limit, but if we set a time limit to load the web page using waits. Page load exception occurs if the page is not loaded within time.
Screen Shot Exception
This exception raised by selenium web driver when the selenium is unable to take the screenshot or capture the current screen.
solution : Allocate more memory to Selenium while initializing the grid
Refresh the page before taking screenshot.
Unexpected Alert Present Exception
This Selenium exception happens when there is the appearance of an unexpected alert.