How to Find an Element by XPath in Selenium

Today's post aims to answer a pretty straightforward question: How do I find an element by XPath in Selenium? So,…

By Testim,

Today’s post aims to answer a pretty straightforward question: How do I find an element by XPath in Selenium? So, why is this a question worth tackling? What is XPath, and why should you care about it?

Here on this blog, we’re all about software testing. Selenium is one of the most popular tools for testing web applications. One of the most important aspects of using Selenium is how you locate elements to interact with them. You can find elements by their ID, text content, the text of their links, and many more. Today, we’ll cover another important way in which you can locate elements, showing you how to find an element by XPath in Selenium.

XPath (XML Path Language) is a World Wide Web Consortium (W3C) standard. It is a query language used for selecting nodes from an XML document. So, what does that have to do with testing webpages? Everything, since XPath is the language used to navigate through the tag structure of a webpage. Because of that, we’ll start the post by covering XPath, its definition, and its types. Only then will we proceed to show you how to use Selenium to find elements via XPath.

Let’s get to it.

Understanding XPath

As promised, let’s start by covering XPath in more detail. You’ll learn XPath’s definition, its types, and its syntax. We’ll also show you how to quickly and easily perform XPath queries using Google Chrome’s developer tools. Let’s get started.

What Is XPath?

As mentioned before, XPath is a query language you can use both on XML and HTML documents. You can use XPath to navigate through the elements of a webpage and find them. That’s why XPath is relevant in the context of Selenium testing: It’s yet another way of locating elements, which is essential when trying to interact with them in an automated manner.

What Are the Different Types of XPath?

XPath comes in two different flavors: absolute XPath and—you’ve guessed it!—relative XPath. Let’s now see how the two compare.

Absolute XPath

The absolute variation of XPath has this name because it starts from the root element of a document. When writing an absolute XPath, you start with a single forward-slash (/) and walk your way until you find the desired element.

Absolute XPaths have a serious downside. Any automation that relies on one will fail if something changes in the path, leading to fragile tests.

Let’s now see an example of what an absolute path looks like. Head to the example.com website using the Chrome web browser. Take a look at the source code of the page. It should look like this:

selenium find element by xpath

As you can see, it’s not a lot, which makes it perfect for our purposes. Now, press the F12 key to fire up the developer tools. Head to the Console tab and paste the following on it:

Then, press Enter, and you should see that the correct element is correctly identified:

selenium find element by xpath

Let’s now learn about the relative XPath properties and see how it compares to the absolute variant.

Relative XPath

As its name suggests, the relative XPath isn’t a single hierarchical path from the root element of the HTML document object model (DOM) structure. Instead, you can use a relative path to find an element in any part of the DOM. Let’s see what that looks like in practice.

Unlike its absolute counterpart, the relative XPath starts with a double forward-slash (//). Then, you can use the XPath syntax to find the element you’re searching for. The complete relative XPath expression to find the h1 element from the previous example would be //h1. Since there’s just one h1 element on the whole page, that’s all that is needed.

So, let’s go back to Chrome’s developer tools. Head back to the console and paste the following command on it:

Then, press Enter. As before, you’ll see the element is shown in the result:

selenium find element by xpath

Selecting Between Elements Using Attributes

In the previous example, we had just a single h1 element. But what if there were many of them? A solution would be to perform additional filtering by using the element’s attributes—such as the ID, class name, link text, and more—and applying the following syntax:

Consider the following HTML document:

As you can see, we have three div elements. Let’s say you want to select the second one. You could do it by using the ID attribute, like this:

The equivalent absolute XPath would be:

Selecting Between Elements by Index

What if the div tags from the previous example didn’t contain IDs? How could we tell them apart using XPath? The answer is simple: we can use an index. Here’s the XPath code to select the third div using relative XPath:

Remember that, unlike most programming languages you’re used to, indexes in XPath start with 1.

How to Find an Element by XPath in Selenium

You’ve just learned about XPath and how it works. But how can you leverage XPath in Selenium? That’s easy. With your favorite programming language, you have to use the By.xpath method on the web driver object. The following example was written in JavaScript:

The example above finds the h1 element from the example.com website, using an absolute XPath. The following example retrieves the same element, but this time using the relative XPath:

The Pros and Cons of Using XPath to Locate Elements

Selenium has its pros and cons, as any tool does. XPath is certainly no exception to that.

The main pro of finding elements via XPath is that it allows you to interact with dynamic elements, which are impossible or hard for you to locate via IDs or other attributes.

The main cons of using XPath in testing relate to the robustness of test cases. Since XPath relies on attributes of the elements that can be changed, tests that use XPath for element location can suddenly fail when those elements are updated. This causes a scenario where test maintenance is a burden.

Conclusion

I’ve started this post by calling Selenium “one of the most popular tools for testing web applications.” That’s not completely correct. Selenium isn’t an automated testing tool per se, or at least it wasn’t intended as such. Rather, Selenium is a web automation tool. With it, you can automate a lot of different tasks you can do with browsers. You could do it to automate tedious, repetitive administrative tasks, for instance. But who are we fooling, right? Its most common use is indeed testing. And in the context of testing web applications, locating elements on the page is one of the most fundamental tasks you need to perform.

As we’ve mentioned, Selenium does offer several different ways in which to find elements. In this post, we’ve covered how to find elements in Selenium using XPath—and why do it. You’ve learned about absolute and relative XPath and how they compare to each other. Finally, you’ve learned about the downsides of XPath in Selenium, especially how it can lead to brittle tests. Is there a way out of this problem?

As it turns out, there is. And the answer is to leverage better tools. Sure, “better” can be highly subjective, but we have a straightforward definition. For us, better tools mean smarter tools—tools that use more resilient approaches for locating elements so that test cases don’t break easily when those elements have their attributes changed.

One such tool is Testim. Testim makes sure your tests remain robust with its AI-based locators and don’t fail with each small change to the application. Testim accomplishes this by inspecting the complete DOM structure of the web app. When doing that, Testim identifies many different attributes it uses to uniquely identify each element from the webpage. That way, it can detect when one or more of the element’s attribute changes while still keeping the test valid.

We invite you to take a look and give Testim a try today. Thanks for reading, and until the next time.

What to read next

A Detailed Look at Selenium Pros and Cons

How to Fix Selenium’s “Element Is Not Clickable at Point”

Testim's latest articles, right in your inbox.

From our latest feature releases, to the way it impacts the businesses of our clients, follow the evolution of our product

Blog Subscribe