Most Underrated WebDriver Locator – XPath






4.88/5 (12 votes)
Most Underrated WebDriver Locator – XPath
Introduction
One of the most underrated locators in Selenium WebDriver or any other Web Automation Framework is the XPath locator. Most probably, people don't use XPath because they think that it's too hard to learn or write. Earlier, generating XPath used to be a tedious task but now with the help of Chrome Developer Tools, Firebug and other tools, it became hell easy.
If you are not familiar with Selenium WebDriver, I suggest you to read my previous post "Getting Started with WebDriver C# in 10 Minutes". Here, I will show you how you can use the full power of the XPath Locators to find the hardest to locate elements.
What Is XPath?
- XPath is a syntax for defining parts of an XML document.
- XPath uses path expressions to navigate in XML documents.
- XPath contains a library of standard functions.
- XPath is a major element in XSLT.
- XPath is a W3C Recommendation.
XPath Expressions
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="en">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
/bookstore/book[1]
- Selects the firstbook
element that is the child of thebookstore
element/bookstore/book[last()]
- Selects the lastbook
element that is the child of thebookstore
element/bookstore/book[last()-1]
- Selects the lastbook
element that is the child of thebookstore
element/bookstore/book[position()<3]
- Selects the first twobook
elements that are children of thebookstore
element//title[@lang]
- Selects all thetitle
elements that have an attribute namedlang
with a value of ‘en
’/bookstore/book[price>35.00]
- Selects all thebook
elements of thebookstore
element that have aprice
element with a value greater than35.00
/bookstore/book[price>35.00]/title
- Selects all thetitle
elements of thebook
elements of thebookstore
element that have aprice
element with a value greater than35.00
XPath Axes
axisname::nodetest[predicate]
ancestor
- Selects all ancestors (parent, grandparent, etc.)descendant
- Selects all descendants (children, grandchildren, etc.)following-sibling
- Selects all siblings after the current nodepreceding-sibling
- Selects all siblings before the current nodechild
- Selects all children of the current nodeparent
- Selects the parent of the current nodeattribute
- Selects all attributes of the current nodeancestor
- Selects all ancestors (parent, grandparent, etc.)descendant
- Selects all descendants (children, grandchildren, etc.)
In order to demonstrate the power of the XPath Axes, I’m going to use the following page: http://sharepoint.telerik.com/aspnet-ajax/web-parts/Pages/Single-List-Binding.aspx.

We want to locate the td
element with Book Author for the book with id = 17. You can open Chrome Developer Tools via F12 key. When you hit CTRL + F, the search form, marked with yellow on the image above will be displayed. There, you can test your XPath expressions. In order to find the book author, we can use the following expression: “//td[contains(text(),’17’)]/following-sibling::td[2]
“. Which means, find the second td
element which is below the td
which contains the text “17
“.
Also, another very useful and often used XPath expression: “//div[contains(id(),’myElementPartId’)]
”. You can use it to locate elements which have dynamically generated IDs.
WebDriver XPath Example
I will use the following page for my next example: http://www.tutorialspoint.com/html/html_tables.htm.

In the test below, we want to find the name of the man with a salary which is equal to 5000
.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace AutomateThePlanetWebDriver
{
[TestClass]
public class XpathExpressions
{
public IWebDriver Driver { get; set; }
public WebDriverWait Wait { get; set; }
[TestInitialize]
public void SetupTest()
{
this.Driver = new FirefoxDriver();
this.Wait = new WebDriverWait(this.Driver, TimeSpan.FromSeconds(30));
}
[TestCleanup]
public void TeardownTest()
{
this.Driver.Quit();
}
[TestMethod]
public void Find_Column_Table_XPath()
{
this.Driver.Navigate().GoToUrl(@"www .tutorialspoint.com/html/html_tables.htm");
var expression = By.XPath("/html/body/div/div/div/div[2]/div[1]/div/div[6]/table/tbody");
this.Wait.Until(x => x.FindElement(expression));
var element = this.Driver.FindElement(By.XPath
("//td[contains(text(), '5000')]/preceding-sibling::td[1]"));
Assert.AreEqual<string>("Ramesh Raman", element.Text);
}
}
}
In the TestInitialize
method, we initialize the WebDriver
instance and the WebDriverWait
, which we later use to wait for a specific element on the page. This way, we will be sure that the page is completely loaded. Then we use complex XPath Axes Expression in order to find the desired element and lastly assert the inner text of the searched td element.
You can find more details and examples about XPath syntax on the following page: http://www.w3schools.com/xpath/xpath_syntax.asp.
So Far in the 'Pragmatic Automation with WebDriver' Series
- 10 Advanced WebDriver Tips and Tricks Part 2
- 10 Advanced WebDriver Tips and Tricks Part 1
- Create Custom Selenium IDE Export to WebDriver
- Automate Telerik Kendo Grid with WebDriver and JavaScript
- Speed up Selenium Tests through RAM Facts and Myths
- Microsoft Edge WebDriver- What Everybody Ought to Know About
- Test URL Redirects with WebDriver and HttpWebRequest
- WebDriver Selenium Tor Integration C# Code
- Most Underrated WebDriver Locator - XPath
- Getting Started with WebDriver C# in 10 Minutes
If you enjoy my publications, feel free to SUBSCRIBE
Also, hit these share buttons. Thank you!
Source Code
All images are purchased from DepositPhotos.com and cannot be downloaded and used for free.
License Agreement