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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })
  • React Callback Refs: What They Are and How to Use Them
  • Process Mining Key Elements

Trending

  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink

Selenium Webdriver Exceptions

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

By 
Abhishek Gaur user avatar
Abhishek Gaur
·
Nov. 08, 18 · Tutorial
Likes (15)
Comment
Save
Tweet
Share
61.9K 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.

Related

  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })
  • React Callback Refs: What They Are and How to Use Them
  • Process Mining Key Elements

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: