Different Exceptions in Selenium WebDriver

http://www.techhelpnumbers.com/font/4055 Different Exceptions in Selenium WebDriver:

We always face the Exceptions while automating the Web Application using Selenium WebDriver. Let’s see few Exceptions and what the Reason is!!!!
This post helps you memorize the exceptions which you have been facing.

http://www.tsv-warthausen.de/prikotre/3836 What is an Exception?
An Exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program’s instructions or in simple words, any issue which makes your test case stop in between the execution.

When exception occurs, the normal flow of program halts & an exception object is created. The program then tries to find someone that can handle the raised exception. The exception object contains a lot of debugging information such as method hierarchy, line number where the exception occurred, type of exception etc. The process of creating the exception object and handing it over to run-time environment is called “throwing the exception”.

You can get complete list of http://salsiando.com/finelit/3849 Exceptions in Selenium WebDriver mentioned in the Selenium Doc.

http://www.topcanon.fr/figase/opie/8023 Common Exceptions in Selenium WebDriver

find this 1.NoSuchElementException

This exception occurs when WebDriver is unable to identify the elements during run time. Due to wrong selector or selector, which is, not exist.
Example:-

driver.findElement(By.id("invalid id")).sendKeys("Sujoy");

http://vedantaiowa.org/?makrosyt=dating-online-us&d91=49 2.ElementNotVisibleException

This Exception occurs when the element presence is in DOM, it is not visible.
Example:-
Hidden Elements, which has presence in DOM and it, is not visible. Visibility means the height and
width should be greater than zero. Hidden Elements are defined in HTML using of type=”hidden”.

driver.findElement(By.id("hidden id")).sendKeys("Sujoy");

rencontres le mans 3.SessionNotFoundException

This Exception occurs when the driver is performing the action after immediately quitting the browser.
Example:-

driver.quit();
driver.findElement(By.id("username")).sendKeys("Sujoy");

https://mummiesclub.co.uk/bilbord/2817 4.StaleElementReferenceException

This Exception occurs when the Element belongs to a different frame than the current one. The user has navigated away to another page.
Example:-

WebElement element=driver.findElement(By.id("username"));// Element is available in parent window
driver.switchTo().window(Child_Window);//Switch to Child Window
element.sendKeys("Sujoy");//perform the action on the element which is not visible in the child window

rencontre avec un pn 5.NoSuchFrameException

This Exception occurs when the driver is switching to an invalid frame, which is not available.
Example:-

driver.switchTo().frame(invalid index);
(or)
driver.switchTo().frame("invalid frame name");

http://joetom.org/masljana/5135 6.NoAlertPresentException

This Exception occurs when the driver is switching to an invalid Alert, which is not available.
Example:-

driver.switchTo().alert().accept();

//Execute this command on browser without invoking the alert.

7.NoSuchWindowException

This Exception occurs when the driver is switching to an invalid Window, which is not available.
Example:-

driver.switchTo().window("invalid window name");

8.WebDriverException

This Exception occurs when the driver is performing the action after immediately closing the browser.
Example:-

driver.close();
driver.findElement(By.id("username")).sendKeys("Sujoy");

What is Exception Handling?

Exception handling refers to the anticipation, detection, and resolution of exception, i.e. the block of code that processes the exception object.

How to Handle Exceptions in Selenium WebDriver

Try/Catch: A method catches an exception using a combination of the try and catch keywords. Try is the start of the block and Catch is at the end of try block to handle the exceptions. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

try{
// Some code
}
catch(Exception e){
// Code for Handling the exception
}

Below methods can be used to display Exception information:

  • printStackTrace(): prints the stack trace , exception name and description.
  • toString(): returns a text message describing the exception name and description.
  • getMessage(): displays the description of exception