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.
|<?xml version=“1.0“ encoding=“UTF-8“?>|
|<title lang=“en“>Harry Potter</title>|
|<title lang=“en“>Learning XML</title>|
- /bookstore/book – 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
- /bookstore/book[last()-1] – 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
- //title[@lang] – 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
- /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
- ancestor – Selects all ancestors (parent, grandparent, etc.).
- descendant – Selects all descendants (children, grandchildren, etc.).
- following-sibling – Selects all siblings after the current node.
- preceding-sibling – Selects all siblings before the current node.
- child – Selects all children of the current node.
- parent – Selects the parent of the current node.
- attribute – Selects all attributes of the current node.
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 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“. 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
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 that is equal to 5000.
|public class XpathExpressions|
|private IWebDriver _driver;|
|public void SetupTest()|
|_driver = new FirefoxDriver();|
|_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);|
|public void TeardownTest()|
|public void Find_Column_Table_XPath()|
|var element = _driver.FindElement(By.XPath(“//td[contains(text(), ‘5000’)]/preceding-sibling::td“));|
|Assert.AreEqual(“Ramesh Raman“, element.Text);|
You can find more details and examples about XPath syntax on the following page or this one.