What are the various important exceptions in Selenium?


The various important exceptions in Selenium are listed below −

  • TimeOutException − This exception is thrown if an action does not complete in a particular duration. If the page element does not load even after the waits.

driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS) ;
driver.get(“https://www.tutorialspoint.com/index.htm” );

In the above program, an implicit wait of 15 seconds is added. If the page https://www.tutorialspoint.com/index.htm doesn’t load in 15 seconds, then TimeoutException will be thrown.

  • NoSuchElementException − This exception happens if the web element with particular attributes does not exist on the page. This exception class is a subclass of NotFoundException and is thrown if the driver is not successful in locating the elements.

driver.findElement(By.name("tutorial-test")).click();
//Exception Handling:
try {
   driver.findElement(By.name("tutorial-test")).click();
} catch (NoSuchElementException e)
  • ElementNotVisibleException − This exception happens if the web element is not visible but it is present in DOM. It is a subclass of ElementNotInteractable class. In the scenarios when the driver tries to perform an action on an element which is not visible or hidden, this exception is thrown. Here, this exception happens if the element with attribute name tutorial-test is hidden.

driver.findElement(By.name("tutorial-test")).click();
//Exception Handling:
try {
   driver.findElement(By.name("tutorial-test")).click();
} catch (ElementNotVisibleException e)
  • StaleElementException - This exception happens if the web element is not present because it either got deleted or not attached with DOM anymore. There is a minor difference with this exception from ElementNotVisibleException.

    In a few incidents, it happens that a specific web element was created on the page without any issues, however currently it is absent may be because of DOM refresh, a new navigation page got added or switching to frame or window.

Updated on: 10-Jun-2020

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements