Showing posts with label Exceptions in Selenium WebDriver. Show all posts
Showing posts with label Exceptions in Selenium WebDriver. Show all posts

Sunday, November 18, 2018

Different Types of Exceptions in Selenium WebDriver - Selenium WebDriver Tutorial

In this tutorial, I am going to cover how to handle exceptions in Selenium WebDriver.



Topics to be covered -


  • What is Exception?
  • Different WebDriver Exceptions.







Exceptions in WebDriver







What is Exception?

An exception is an event that occurs during the execution of a program. It interrupts the normal flow of execution of the program.

Whenever an exception occurs the system creates an exception object that contains detailed information of the exception and throws it i.e., it halts the normal execution flow and displays information about the exception on the console window.

If we do not want such abnormal termination of the execution of the program, handling these exceptions is necessary.

Handling exception means to avoid abnormal halts of the program flow and allowing the execution of the program even after the occurrence of an exception. This ensures the execution of the entire program.



Different Exceptions in Selenium WebDriver - 

Below is the list of most commonly exceptions occurred while creating Selenium Webdriver scripts - 
  • ElementNotVisibleException.
  • ElementNotSelectableException.
  • NoSuchElementException. 
  • ElementNotClickableException.
  • NoSuchFrameException.
  • NoAlertPresentException.
  • NoSuchWindowException.
  • TimeoutException.
  • WebDriverException.
  • SessionNotFoundException.


1. NoSuchElementException - This exception occurs when you are trying to locate an element using wrong locators.

In case if you don't know how to locate an element in Selenium follow the link below - Locating the elements in Selenium










In the above images, we are trying to inspect 'Email or Phone' field of the Facebook application whose locator is - id="email" 




 package com.sessions;  
   
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
   
 public class SeleniumExceptions {  
   
      public static void main(String[] args) {  
           System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
             
           WebDriver driver = new ChromeDriver();  
             
           driver.get("https://www.facebook.com");//launch facebook application.  
             
           driver.manage().window().maximize();//maximizing browser.  
             
           driver.findElement(By.id("email123")).sendKeys("abcd1234");  
   
   
      }  
   
 }  
   


Note In the above program we used id="email123" which is a wrong locator so you will get below exception -






2. NoAlertPresentException - This exception occurs when you are trying to switch to an alert/pop-up window which is not available.


 package com.sessions;  
   
 import org.openqa.selenium.Alert;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
   
 public class SeleniumExceptions {  
   
      public static void main(String[] args) {  
           System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
             
           WebDriver driver = new ChromeDriver();  
             
           driver.get("https://www.facebook.com");//launch facebook application.  
             
           driver.manage().window().maximize();//maximizing browser.  
             
           Alert alert = driver.switchTo().alert();  
   
   
      }  
   
 }  
   


Note - In the last statement of the above program we are trying to switch to an alert after opening the login page of Facebook. But there is no such alert window appears when you open the Facebook application. So you will get below exception -







3. NoSuchFrameException - This exception occurs when you are trying to switch to a frame which is not available.


 package com.sessions;  
   
 import org.openqa.selenium.Alert;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
   
 public class SeleniumExceptions {  
   
      public static void main(String[] args) {  
           System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
             
           WebDriver driver = new ChromeDriver();  
             
           driver.get("https://www.facebook.com");//launch facebook application.  
             
           driver.manage().window().maximize();//maximizing browser.  
             
           driver.switchTo().frame("framename");  
   
   
      }  
   
 }  
   



Note - In the last statement of the above program we are trying to switch to a frame after opening the login page of Facebook. But there is no such frame when you open the Facebook application. So you will get below exception -






4. NoSuchWindowException - This exception occurs when you are trying to switch to a window which is not available.


 package com.sessions;  
   
 import org.openqa.selenium.Alert;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
   
 public class SeleniumExceptions {  
   
      public static void main(String[] args) {  
           System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
             
           WebDriver driver = new ChromeDriver();  
             
           driver.get("https://www.facebook.com");//launch facebook application.  
             
           driver.manage().window().maximize();//maximizing browser.  
             
           driver.switchTo().window("Homepage");  
   
   
      }  
   
 }  
   


Note - In the last statement of the above program we are trying to switch to a window "Homepage" after opening the login page of Facebook. But there is no such window when you open the Facebook application. So you will get below exception -








5.  NoSuchSessionException - This exception occurs when the driver is performing any action after immediately quitting the browser.




 package com.sessions;  
   
 import org.openqa.selenium.Alert;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
   
 public class SeleniumExceptions {  
   
      public static void main(String[] args) {  
           System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
             
           WebDriver driver = new ChromeDriver();  
             
           driver.get("https://www.facebook.com");//launch facebook application.  
             
           driver.manage().window().maximize();//maximizing browser.  
             
           driver.quit();  
             
           driver.findElement(By.id("email"));  
   
   
      }  
   
 }  
   


Note - In the last statement of the above program we are trying to locate 'Email or Phone' text field after quitting the browser. So you will get below exception -





6. ElementNotVisibleException - An element is present in the HTML structure but it is not visible on the web-page because it is hidden.

Hidden elements are defined in the HTML using type='hidden'.


7. ElementNotSelectableException - An element is present in the HTML structure but it is disabled(cannot click or select) on the web-page.


8. StaleElementRefrenceException - The referenced element is no longer present on the DOM page (a reference to an element is now Stale). E.g. The Element belongs to a different frame than the current one OR the user has navigated away to another page.

9. TimeoutException - This Exception occurs when the driver fails to locate an element in the specified time in the script.


 package com.sessions;  
   
 import java.util.concurrent.TimeUnit;  
   
 import org.openqa.selenium.Alert;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.openqa.selenium.support.ui.ExpectedConditions;  
 import org.openqa.selenium.support.ui.Wait;  
 import org.openqa.selenium.support.ui.WebDriverWait;  
   
 public class SeleniumExceptions {  
   
      public static void main(String[] args) {  
           System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
             
           WebDriver driver = new ChromeDriver();  
             
           WebDriverWait wait = new WebDriverWait(driver,10);  
             
     driver.get("https://www.facebook.com");//launch facebook application.  
             
           driver.manage().window().maximize();//maximizing browser.  
             
           WebElement email = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email123")));  
             
           email.sendKeys("abcsd2344");  
             
             
             
   
   
      }  
   
 }  
   


Note - In the above program we have specified 10 seconds time. We are trying to locate 'email' element with the wrong locator 'email123'. So if within 10 seconds the driver will not be able to locate the element, it will throw below exception -




That's all!!! 

Please give your comment below if you have any doubt.