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
="1.0"="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 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 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 all the title
elements that have an attribute named lang
with a value of ‘en
’ /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
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 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 ancestor
- 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
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