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

/bookstore/book[1] – Selects the first book element that is the child of the bookstore element.

Selects the first book element that is the child of the bookstore element. /bookstore/book[last()] – Selects the last book element that is the child of the bookstore element

Selects the last book element that is the child of the bookstore element /bookstore/book[last()-1] – Selects the last – 1 book element that is the child of the bookstore element

Selects the last – 1 book element that is the child of the bookstore element /bookstore/book[position()<3] – Selects the first two book elements that are children of the bookstore element

Selects the first two book elements that are children of the bookstore element //title[@lang] – Selects the lang attribute of the current node

Selects the lang attribute of the current node /bookstore/book[price>35.00] – Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00

Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 /bookstore/book[price>35.00]/title – Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

XPath Axes

ancestor – Selects all ancestors (parent, grandparent, etc.).

Selects all ancestors (parent, grandparent, etc.). descendant – Selects all descendants (children, grandchildren, etc.).

Selects all descendants (children, grandchildren, etc.). following-sibling – Selects all siblings after the current node.

Selects all siblings after the current node. preceding-sibling – Selects all siblings before the current node.

Selects all siblings before the current node. child – Selects all children of the current node.

Selects all children of the current node. parent – Selects the parent of the current node.

Selects the parent of the current node. attribute – Selects all attributes of the current node.

To demonstrate the power of the XPath Axes

We want to locate the td element with Book Author for the book with id = 17. You can open Chrome Developer Tools via the 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 that is below the td that contains the text “17“.

Also, another very useful and oftenly used XPath expression: “//div[contains(id(),’myElementPartId’)]”. You can use it to locate elements which have dynamically generated IDs.

WebDriver XPath Example

In the test below, we want to find the name of the man with a salary that is equal to 5000.

In the TestInitialize method, we initialize the WebDriver instance and set the implicit wait timeout so that we will wait until the elements are displayed. This way we will be sure that the page is completely loaded. Then we use complex XPath Axes Expression to find the desired part and lastly assert the inner text of the searched td element.