Check out myAngular article series with live demos and Facebook group Angular - Advanced Topics

How to Create Xpath Using Linq

Torgeir "Tor" Helgevold
- JavaScript Developer and Blogger
Published: Sun May 10 2015

As an alternative to my previous xpath library, XpathItUp, I've created a lambda based framework (LambdaToXpath) for generating xpath expressions. The goal is to improve and address some of the short comings of my previous library.

The primary use case for the library is generating xpath expressions for automated testing (e.g. Selenium Web Tests), but LambdaToXpath doesn't depend on any third party libraries, and can be used to generate generic xpath expressions.

The most obvious difference from XpathItUp is that LambdaToXpath let's you describe xpath expressions through C# lambda expressions rather than a fluent method chain. Another difference is that the generated expressions are centered around a specified target element, which offers more granular control in cases where you need a specific node to be returned. One example of this is selecting elements to click in web tests.

LambdaToXpath is available on Nuget
PM> Install-Package LambdaToXpath

Single Element

var xpath = CreateXpath.Where(e => e.TargetElementName == "td"); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.TargetElementText == "my test"); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.TargetElementText.Contains("my test")); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.TargetElementText.Contains("my test") == false); var xpath = CreateXpath.Where(e => e.TargetElementName == "span" && e.TargetElementText.StartsWith("Test")); var xpath = CreateXpath.Where(e => e.TargetElementName == "span" && e.TargetElementText.EndsWith("Test"));

Attributes

var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Attribute("id").Contains("_parent_child") && e.Attribute("class").Text == "myClass"); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Attribute("id").Contains("_parent_child") == false);

Parent

var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Parent.Name == "tr"); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Parent.Name == "tr" && e.Parent.Attribute("id").Text == "_parent_child" && e.Parent.Attribute("class").Text == "myClass"); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Parent.Name == "tr" && e.Parent.Text == "test"); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Parent.Name == "tr" && e.Parent.Text.Contains("myTest")); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Parent.Name == "tr" && e.Parent.Attribute("id").Contains("_parent_child") == false); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Parent.Name == "tr" && e.Parent.Text.Contains("_parent_child") == false); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Parent.Name == "tr" && e.Parent.Text.StartsWith("test") == true); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Parent.Name == "tr" && e.Parent.Text.EndsWith("test") == true);

Ancestor

var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Ancestor.Name == "table");

Descendant

var xpath = CreateXpath.Where(e => e.TargetElementName == "table" && e.Descendant.Name == "td");

Siblings

var xpath = CreateXpath.Where(e => e.TargetElementName == "b" && e.PrecedingSibling.Name == "a" && e.FollowingSibling.Name == "c");

Position

var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Position == 5); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Position == 4 && e.Parent.Position == 5 && e.Parent.Name == "tr");

Select attributes of Target Element

var xpath = CreateXpath.Where(e => e.TargetElementName == "td").Select(e => e.Text); var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Attribute("class").Text == "myClass").Select(e => e.Attribute("class")); var xpath = CreateXpath.Where(e => e.TargetElementName == "td").Select(e => e.TextAt(1));

In all cases an xpath expression returning the target element itself, or an attribute of the target element, is generated.

LambdaToXpath is available on Nuget
PM> Install-Package LambdaToXpath

If you liked this article, share it with your friends on social media:

We also have a new Facebook group about advanced Angular topics.

I invite you to follow me on twitter