Selenium WebDriver
Benefits of Selenium
Four components of Selenium
Difference b/w Selenium IDE, RC & WebDriver
Selenium WebDriver Architecture
Background when user execute selenium code
Download and Install Java
Download and Install Eclipse
Download Selenium WebDriver
Selenium WebDriver Locators
Selenium - Launch Browser
Selenium WebDriver Waits
Selenium- Implicit wait
Selenium- Explicit wait
Selenium- Fluent wait
Selenium- Commonly used commands
Selenium- findElement & findElements
Selenium- Selenium-Handling check Box
Selenium- Handling Radio button
Selenium- Handling drop down
Selenium- Take Screenshot
Selenium- Handle Web Alerts
Selenium- Multiple Windows Handling
Selenium- Handle iframes
Selenium- Upload a file
Selenium- Download a file
Selenium- Actions Class Utilities
Selenium- Mouse Actions
Selenium- Keyboards Events
Selenium- Handle mouse hover Actions
Selenium- Drag and Drop
Selenium- Scroll a WebPage
Selenium- Context Click / Right Click
Selenium- Double Click
Selenium- Desired Capabilities
Selenium- Assertions
Selenium- Exceptions and Exception Handling
Selenium- Difference b/w driver.close() & driver.quit()
Selenium- difference b/w driver.get() & driver.navigate()
Selenium- JavascriptExecutor
Selenium- Read excel file using Fillo API
Selenium- Database Testing using Selenium
Selenium- Read & write excel file using Apache POI
Selenium- Read and Write csv file in Selenium
Selenium- Dynamic Web Table Handling
Selenium- Maven Integration with Selenium
Selenium- Set up Logging using Log4j
Selenium-Implement Extent Report
Exceptions in Selenium WebDriver (Common Exceptions)
This article will present you with a complete idea about Exceptions , How to handle Exceptions in selenium and Common exceptions which we get in selenium
We will learn below topics in this article
What is an Exception?
Java exception is an event which is created when code is not able to complete normally, it is an error situation which a programmer can handle while writing the code but this may occur at run time of program as well.
There may be several reasons behind the occurrence of exceptions which indicate the halt in the program flow.
In java to handle such cases where an unexpected event happens in code we have 3 categories of exception.
-
- Checked Exception or Compile Time Exception
- Unchecked Exception or Runtime Exception
- Error
Java Checked Exception
Checked exceptions can be observed while compiling any java code, java compiler can determine based on used java classes if there will be an exception or not and we have to handle those exceptions or throw them to parent or caller.
Few checked exceptions are: FileNotFoundException, IOException, SQLException etc.
Java Unchecked Exception
Unchecked exceptions or runtime exceptions are those exceptions which occur when we execute or run java programs. The reason they are called unchecked is because java compilers can’t check them while compiling, it is not like checked exceptions.
Few examples of run time exceptions are NullPointerException, ArrayIndexOutOfBoundsException, InvalidArgumentException and etc.
Java Errors
If some event running JVM is not able to work properly then that event will be called an error, error is not an exception and can’t be handled, we may try to code in such a way that error never comes but still they may come.
Common Exceptions in Selenium WebDriver
NoSuchElementException
The exception occurs when WebDriver is unable to find and locate elements.
ElementNotVisibleException
This Exception occurs when WebDriver tries to perform an action on an invisible web element and it is not interactable.
ElementNotSelectableException
This Selenium exception occurs when an element is presented in the DOM, but unable to select
NoSuchFrameException
This Exception occurs When WebDriver is trying to switch to an invalid frame
NoAlertPresentException
This Exception occurs when WebDriver is trying to switch to an invalid frame or alert not present
NoSuchWindowException
This Exception occurs when WebDriver tries to switch to an invalid window.
StaleElementReferenceException
When a referenced element is no longer present on the DOM page or element is decayed or stale.
For example, the element to be accessed is no longer a part of the current page after refreshing a web page.
SessionNotFoundException
This exception occurs when we quit the browser and webdriver is in active mode.
TimeoutException
This exception occurs when the command that is currently in execution does not complete within the expected time frame.
WebDriverException
This Exception occurs when the WebDriver is acting after closing the browser.
ElementNotInteractableException
This Selenium exception occurs when an element is present in the DOM and it is impossible to interact with such an element.
InvalidElementStateException
Indicates that a WebElement is in a state that means actions cannot be performed with it. Examples would include an element being obscured by another when clicking, or perhaps not being visible on the DOM. Subclasses of this provide more detailed information
JavascriptException
This issue occurs while executing JavaScript given by the user.
NoSuchAttributeException
This exception occurs when the attribute of an element could not be found.
MoveTargetOutOfBoundsException
Indicates that the target provided to the actions move() method is invalid – outside of the size of the window.
RemoteDriverServerException
This Selenium exception is thrown when incorrect combinations/fields in browser capabilities leads to the server not sending a response
SessionNotCreatedException
This exception occurs when a new session could not be successfully created.
UnableToSetCookieException
This exception occurs if a driver is unable to set a cookie.
UnhandledAlertException
This expectation occurs when there is an alert, but WebDriver is not able to perform Alert operation.
UnexpectedAlertPresentException
This exception occurs when there is the appearance of an unexpected alert.
UnreachableBrowserException
Indicates there was a problem communicating with the browser being controlled or the Selenium server. The most common causes for this exception are:
-
- The provided server address to RemoteWebDriver is invalid, so the connection could not be established.
- The browser has died mid-test.
Exception Handling in Selenium
To handle checked or compile time exception there are two approaches:
- Catch Exception – In this approach wherever we see exceptions may occur we can add try and catch blocks to catch them, we will see details in next section.
- Throw/Throws– This is a second approach where we may simply throw an exception from a method where it is coming and let the next class or JVM handle it, this may lead to errors if not handled later.
Some of you may think what about unchecked exceptions or errors? normally programmers are just careful about them and create logic in such a way that it never occurs.
Try Catch
Try catch block is a way to handle exceptions in java by catching the exception and then taking appropriate measures.
In the try block of code we write normal code and if there is an exception code will leave the execution and go to the catch block where we take action based on the exception.
Simple Syntax For Try Catch:
try { //Some code which may throw exception } catch(Exception ex) { //here we catch exceptions and do some logging or any other logic. }
Try with Multiple Catch
If a block of code may throw multiple types of exceptions we can add more than one catch block, so there can be only one try and multiple catch blocks.
Syntax For Try and Multiple Catch:
try { //Some code which may throw exception } catch(ExceptionType ex) { //here we catch exceptions and do some logging or any other logic. } catch(ExceptionType2 ex) { //here we catch exceptions and do some logging or any other logic. }
Throws and Throw
This is the second approach to handle exceptions where we throw the exception and the method which throws will use throws keyword and type of exception to throw it.
 Throw: Â
Throw keyword is used in any method when we want to throw some exception or custom exception.
public static void function1() throws Exception{ try{ // write your code here   }catch (Exception e){ // Do whatever you want to perform // throw(e); } }
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 }
Finally:
The Final keyword is used to create a block of code under the try block. This final code block always 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. }
Â
Selenium WebDriver Tutorials
Benefits of Selenium
Four components of Selenium
Difference b/w Selenium IDE, RC & WebDriver
Selenium WebDriver Architecture
Background when user execute selenium code
Download and Install Java
Download and Install Eclipse
Download Selenium WebDriver
Selenium WebDriver Locators
Selenium - Launch Browser
Selenium WebDriver Waits
Selenium- Implicit wait
Selenium- Explicit wait
Selenium- Fluent wait
Selenium- Commonly used commands
Selenium- findElement & findElements
Selenium- Selenium-Handling check Box
Selenium- Handling Radio button
Selenium- Handling drop down
Selenium- Take Screenshot
Selenium- Handle Web Alerts
Selenium- Multiple Windows Handling
Selenium- Handle iframes
Selenium- Upload a file
Selenium- Download a file
Selenium- Actions Class Utilities
Selenium- Mouse Actions
Selenium- Keyboards Events
Selenium- Handle mouse hover Actions
Selenium- Drag and Drop
Selenium- Scroll a WebPage
Selenium- Context Click / Right Click
Selenium- Double Click
Selenium- Desired Capabilities
Selenium- Assertions
Selenium- Exceptions and Exception Handling
Selenium- Difference b/w driver.close() & driver.quit()
Selenium- difference b/w driver.get() & driver.navigate()
Selenium- JavascriptExecutor
Selenium- Read excel file using Fillo API
Selenium- Database Testing using Selenium
Selenium- Read & write excel file using Apache POI
Selenium- Read and Write csv file in Selenium
Selenium- Dynamic Web Table Handling
Selenium- Maven Integration with Selenium
Selenium- Set up Logging using Log4j
Selenium-Implement Extent Report