May 28, 2020

What is XPath in Selenium? The Definitive Guide

Automation
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. 

This blog will explain 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 technique that allows you to navigate the structure of a webpage’s HTML. It also enables you to navigate through a document's XML structure. Using XPath in Selenium helps 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.

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:

XPath=//tagname[@attribute='value']

  • // : 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.

ID

To find the element by the ID of the element.

Classname

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

Name

To find the element by name of the element.

Link Text

To find the element by the text of the link.

XPath

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

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.

StART TRIAL

Back to top