XPath Functions

Jishnu Nambiar
5 min readOct 1, 2023

--

Before exploring XPath functions, it’s recommended to have a basic understanding of XPath fundamentals. You can read the following article to gain a basic understanding of the XPath.

XPath provides a rich set of functions that allow you to manipulate and query XML or HTML documents more effectively. Below is the list of some commonly used Xpath functions

1. text(): This function in XPath is used to precisely match the text content of elements. For example, //label[text()=”Search Amazon.in”] will return all label elements whose text exactly matches “Search Amazon.in”. We can use text() and dot(.) interchangeably for locating elements. correct it

2. contains(): This function in XPath is used to match the text or attribute values partially of the element. For example, //label[contains(text(),”Search ”)] will return all label elements whose text contains “Search”.

Syntax: contains(<text()/@attribute>,<subString>)

contains with function example
contains with attribute example

3. local-name(): This function in XPath is used to get the local part of the name of the node ignoring the namespace. This is useful in a DOM structure that uses a namespace by ignoring it and focusing only on the local name of the element. This is mostly used in the case of svg Tag which usually has its own namespace. for example //*[local-name()=”svg”]

Syntax: //*[local-name=<tagName>]

If you get more matches try to use axes methods

4. starts-with(): This function in XPath is used to match the text or attribute values of the element that starts with the specified string. For example, //label[starts-with(text(),”Sea”)] will return all label elements whose text starts with “Sea”.

Syntax: //tagName[starts-with(<text()/@attribute>,<Start of the String>)]

5. last(): This function in XPath is used to find the last element satisfying the XPath expression. For example, //label[text()=”Search Amazon.in”][last()] will return the last label elements matching the Xpath.

Syntax: <XPath matching the element>[last()]

6. normalize-space(): This function in XPath is used to trim leading and trailing white space and collapsing multiple spaces in between into a single space of values in XPath expression. For example, //label[normalize-space()=”Search Amazon.in”].This XPath expression selects label elements where the normalized text content is equal to “Search Amazon.in”. It comes in handy when we need to deal with variations in spacing within text.

Syntax: //tagName[normalize-space()=”<Value>”]

7. position(): This function in XPath is used to find elements based on their position within the result set. For example, (//input[@class=”nav-input nav-progressive-attribute”])[position()>1] will return the input elements in a position greater than 1 of the result set is returned.

Syntax: (<Xpath>)[position() <Condition> <Value>]

If it would have been Position=1 then it would highlight Search text box

8. substring-before(): This function in XPath is used to extract a portion of a string that appears before a specified delimiter. For example, //label[substring-before(text(), “.”)=”Search Amazon”] will return the label elements whose text content when substring before results in “Search Amazon”

Syntax: //tagName[substring-before(<text()/attribute>,”<delimiterWord>)=”<Text Before delmiter Word>”)]

9. substring-after(): This function in XPath is used to extract a portion of a string that appears after a specified delimiter. For example, //label[substring-after(text(), “.”)=”in”] will return the label elements whose text content when substring before results in “in”.

Syntax: //tagName[substring-after(<text()/attribute>,”<delimiterWord>)=”<Text Before delmiter Word>”)]

10. substring(): This function in XPath is used to extract a portion of a string that appears after a specified delimiter. For example, //label[substring( text(),8,6)=”Amazon”] will return the label elements starting from position 8 and having a length of 6, which is equal to “Amazon”.

Syntax: //tagName[substring(<text()/attribute>,<Starting Position Index>,<Length for the Search>)=”<SubString Value>”)]

Note: Index Starts from 1 and not 0.

11. translate(): This function in XPath is used to replace or remove characters in a string. For example, //label[translate( text(),”.”,”_”)=”Search Amazon_in”] will return the label elements whose text content after replacing dot(.) with Underscore(_) is equal to “Search Amazon_in”.

Syntax: //tagName[translate(<text()/attribute>,“<Words to replaced>”,“<Replacement Word>”)=”<Final Word after Repalcement>”)]

The above methods can be used to change text into Lower or upper case Scenarios.

example for Lower case //label[translate( text(),”SA”,”sa”)=”search amazon.in”] will still select the label shown in the below screenshot.

12. name(): This function in XPath is used to obtain the name of the node, including its prefix and colon if it has one. This function returns the fully qualified name of the node, considering its namespace.

Syntax: //*[name=<tagName>]

Conclusion:

While this article covers only a subset of XPath functions, it’s important to note that XPath offers a wide range of functions for navigating and selecting elements in XML or HTML documents. Drawing from my automation experience, I can assert that the first 6 functions discussed above suffice for the majority of requirements.

In the above examples, we have taken the “Search Amazon.in” label in the textbox, you can see how we wrote XPath for the same element in different ways.

I’d appreciate hearing your thoughts! If you find the content helpful, please consider sharing it with your network and show some love by clapping. Let’s continue learning together! Don’t forget to follow for more informative blogs.

LinkedIn GitHub Medium

“Live as if you were to die tomorrow. Learn as if you were to live forever.” — Mahatma Gandhi

--

--

Jishnu Nambiar

Quality Assurance engineer with overall experience of 4.5.