DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Selenium Webdriver Exceptions

Learn about the different exceptions in Selenium Webdriver and how to handle them.

Abhishek Gaur user avatar by
Abhishek Gaur
·
Nov. 08, 18 · Tutorial
Like (15)
Save
Tweet
Share
61.21K Views

Join the DZone community and get the full member experience.

Join For Free

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 the course of the execution.

When an exception occurs, the normal flow of program halts and 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, the line number where the exception occurred, the type of exception, etc. The process of creating the exception object and handing it over to the run-time environment is called “throwing the exception.”

Common Exceptions in Selenium Webdriver

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

  • ElementNotVisibleException: Although an element is present in the DOM, it is not visible (cannot be interacted with). For example: 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 runtime, 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). For example: 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: Webdriver is performing the action immediately after ‘closing’ the browser.

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

Hope this article can be a quick reference when you need to look at some of the common exceptions in Selenium Webdriver.

Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Connecting Your Devs' Work to the Business
  • Too Many Tools? Streamline Your Stack With AIOps
  • Using JSON Web Encryption (JWE)
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: