Social Blog XPath Selenium
December 28, 2022

What is XPath in Selenium? The Definitive Guide

Continuous Testing

Selenium is a leading test automation framework that enables teams to build comprehensive web tests that navigate through a wide range of web elements. Selenium is free, open source, and allows testing teams to test applications across browsers and platforms. However, implementing a framework with Selenium requires locating a web element. In fact, choosing the right web element locator is a critical part of an effective test automation strategy. 

XPath is one such tool that provides the option to dynamically search a web element. This blog will explore different ways to use XPath in Selenium, a popular path for locating and selecting web elements, as part of your automated testing efforts. 

Read along or jump ahead to whichever section interests you most:




Related Reading: Explore codeless automation testing with Selenium. >>

Back to top

What Is XPath in Selenium?

XPath in Selenium is a way to navigate the structure of a webpage’s HTML, as well as a document's XML structure, to find elements that are not found by locators such as ID, class, or name.

XPath is a syntax for finding elements on web pages, and XPath in Selenium can be used on both HTML and XML documents. There are other, more straightforward Selenium locators available that search for elements using tags or CSS class names. Yet they may not be sufficient to select all DOM elements of an HTML document.

XPath in Selenium at a glance:

  • Enables testing teams to navigate a document's XML structure (which is usable on both XML and HTML documents).
  • XPath in Selenium enables teams to search for a specific element within a web page in a dynamic fashion, which allows for increased flexibility.
  • Although there are other locator options in Selenium, XPath in Selenium allows for teams to select all DOM elements of an HTML document. 

By using XPath, Selenium users can search for a page element in a more dynamic way. This capability gives testers the flexibility to work with locators so they are more advantageous.

XPath in Selenium is just one of many things you need to know about the platform. Explore the following resources to enhance your Selenium skillset.

Back to top

What Is XPath?

XPath is defined as XML path. It is a syntax or language for finding any element on the web page using the XML path expression. XPath is used to find the location of any element on a webpage using HTML DOM structure.

In Selenium automation, if the elements are not found by the general locators like ID, class, name, etc., then XPath is used to find an element on the web page.

In this tutorial, you will learn about XPath in Selenium and different XPath expressions to find complex or dynamic elements whose attributes change dynamically on a refresh of any operations.

The basic format of XPath is explained below.

Syntax for XPath

XPath contains the path of the element situated on the web page. The standard syntax for creating XPath is as follows:


  • // : Select the current node.
  • Tagname: Tagname of the particular node.
  • @: Select attribute.
  • Attribute: Attribute the name of the node.
  • Value: Value of the attribute.
Back to top

Types of XPath Locators

To find the element on web pages accurately, there are a number of different types of locators. Here are a few XPath locator examples, such as ID, name, link text, and CSS path:

XPath Locators

Find different elements on a web page.


To find the element by the ID of the element.


To find the element by the class name of the element.


To find the element by name of the element.

Link Text

To find the element by the text of the link.


XPath is required for finding the dynamic element and traversing between various 
elements of the web page.

CSS path

CSS path also locates elements having no name, class, or ID.

Back to top

XPath Examples

There are two types of XPath:

  • Absolute XPath
  • Relative XPath

Let us explore both XPath types in more detail. 

Absolute XPath

Absolute XPath is the direct way to find the element. But the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath fails.

The key characteristic of XPath is that it begins with the single forward slash(/), which means you can select the element from the root node.
Below is an example of an absolute XPath expression of the element shown in the below screen.

Absolute XPath: /html/body/div[1]/div/div[2]/header/div/div[2]/a/img

Image XPath Selenium 1

Back to top

Final Thoughts on Using XPath in Selenium

As we have learned in this blog, XPath in Selenium is an ideal option for testing teams looks for a dynamic solution that allows testing teams to easily and efficiently locate a web element. Choosing the right web element locator is indeed a crucial component of any effective test automation strategy. XPath in Selenium is a popular path for locating and selecting web elements for good reason.

In this blog we covered many aspects of testing using XPath in Selenium, including a rundown of what XPath in Selenium is and how to go about utilizing this dynamic tool. The examples provided should help kickstart your testing with XPath in Selenium.

XPath in Selenium is a foundational building block for creating a Selenium automation framework. When it comes to XPath in Selenium, there are numerous ways to write using XPath, but choosing the right method will depend on your unique test cases as well as your overall framework. 

Back to top

Try XPath in Selenium With Perfecto

Scale Selenium test automation in the cloud with Perfecto. Perfecto integrates seamlessly with Selenium, and every other tool in your toolchain. With Perfecto and Selenium, you can:

  • Execute web testing 50% faster than other vendors.
  • Enjoy the scalability of Selenium Grid from the cloud.
  • Scale your tests across VMs, real Macs, and multiple platforms.
  • Find and fix bugs quickly with advanced test reporting.
  • Integrate into your entire CI/CD toolchain.

And our Selenium integration is just one of many features that lets Perfecto set you up for success. Perfecto powers exceptional digital experiences by combining the power of flexible test authoring, cross-platform execution, and AI-driven analytics into one secure, cloud-based web & mobile testing platform. 

Experience the world’s largest global testing cloud for web and mobile apps. Try Perfecto free for 14 days.


Back to top