Selenium Webdriver Exceptions

Exception:

Contents

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

Selenium Exceptions

When an 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”.

Selenium web driver exceptions

There is a complete list of Exceptions in Selenium WebDriver mentioned in the Selenium Doc which you may or may not encounter in course of your testing. Hence in this article, we will focus on some most common exceptions in Selenium WebDriver,

  • ElementNotVisibleException: Although an element is present in the DOM, it is not visible (cannot be interacted with). E.g. Hidden Elements – defined in HTML using type=”hidden”.
  • ElementNotSelectableException: Although an element is present in the DOM, it may be disabled (cannot be clicked/selected).
  • InvalidSelectorException: Selector used to find an element does not return a WebElement. Say XPath expression is used which is either syntactically invalid or does not select WebElement.
  • NoSuchElementException: WebDriver is unable to identify the elements during run time, i.e. FindBy method can’t find the element.
  • NoSuchFrameException: WebDriver is switching to an invalid frame, which is not available.
  • NoAlertPresentException: WebDriver is switching to an invalid alert, which is not available.
  • NoSuchWindowException: WebDriver is switching to an invalid window, which is not available.
  • StaleElementReferenceException: The referenced element is no longer present on the DOM page (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.
  • SessionNotFoundException: The WebDriver is performing the action immediately after ‘quitting’ the browser.
  • TimeoutException: The command did not complete in enough time. E.g. the element didn’t display in the specified time. Encountered when working with waits.
  • WebDriverException: The WebDriver is performing the action immediately after ‘closing’ the browser.

What is Exception Handling?

The Exception Handling is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.

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:

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

Hope this article can be a quick reference about common exceptions in Selenium WebDriver.







Best Books available




//phaurtuh.net/4/4429588

Abhishek

10+ years of Diversified experience in Information and communications technology (ICT), specializing in Solution Architect, Software Quality Assurance with proficiency in deployment and development, testing on Client/Server and Web-based applications.

  • Working knowledge in Java8, AWS, GCP, Junit, Mockito, Cobertura, Findbugs, Sonarqube, PMD.
  • Experience in CI/CD using Azure DevOps, Jenkins Pipeline, Github, Nexus, Docker, Kubernetes.
  • Experience in fixing security vulnerability issues using Checkmarx code scanner.
  • Proficient in writing the automation scripts using TestNG, Cucumber, Silk4j
  • Conduct performance test using Silk Performer, Jmeter and SoapUI
  • Experience in finding and fixing JVM memory leak issue using Jmeter, VisualVm.
  • Very good team player with the ability to work in a fast-paced agile development environment as well as an individual contributor.