XPath, which stands for XML Path Language, is a query language widely used in selenium testing and web scraping for writing automation test scripts. It navigates through the XML or HTML documents and finds the node elements present within the structure. Even when the structure is dynamic and complex through XPath, it is easier to identify the web elements and automate the test scripts accordingly.
Selenium locators, while versatile, are often less precise compared to XPath. Other locators, such as ID or CSS selectors, rely on specific attributes like tags and class names to find elements within a document. However, XPath enables testers to select all DOM elements as effectively.
Table of Contents
Why XPath Over Other Selenium Locators?
Several locator tools are present in selenium, one of which is XPath. Other locators in Selenium use techniques that include ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, and CSS Selectors. Meanwhile, XPath offers unique advantages over all the other locators, even though those locators have their strengths and uses.
Advantages of XPath
Below are the advantages of XPath over other Selenium locators:
- Flexibility in Navigating the DOM:
One of the special features of XPath is that it can move forward and backward as well to navigate in the Document Object Model(DOM), which creates the possibility of:
- Traversing up the DOM tree (e.g., selecting a parent or ancestor element).
- Traversing down the DOM tree (e.g., selecting a child or descendant element).
- Traversing sideways in the DOM tree (e.g., selecting a preceding or following sibling).
- Powerful Selection Capabilities:
Xpath contains and supports various types of functions and operators that allow one to write high-level queries for complex documents; some of them are:
- Attribute selection:
- Text content selection:
- Logical operators (and, or):
- Ability to Handle Complex DOM Structures:
The ability to deal with complex and dynamic web pages gives XPath an edge over other Selenium locator tools. It can deal with multiple scenarios, such as:
- Document elements that do not have unique IDs or Names.
- HTML document whose structure is not straightforward.
- Even in cases where elements are deeply nested within multiple levels of the DOM(Document Object Model).
- Positional Selection:
XPath also allows the selection of elements through their position in the document or within a set of siblings.
For example:
- Select the first <li> element:
- Select the last <li> element:
- Select the third <li> element:
- Attribute-Based Selection:
When an element does not have any unique ID, XPath allows one to select even that element if it has a combination of some or multiple attributes which makes it unique For example:
Understanding XPath Syntax
XPath uses a path-like structure syntax to navigate through attributes and elements in an XML or HTML document. The structure of an XPath expression is similar to that of a file path.
Basic Syntax
- Selecting all nodes with a specific tag name:
Example:
Selects all <input> elements.
- Selecting nodes with a specified attribute and value:
Example:
Selects all <input> elements with an id attribute equal to username.
- Selecting nodes containing specific text:
Example:
Select all <button> elements with the text Submit.
Difference Between Static and DynamicXPath
The following are the differences between static and dynamic XPath:
Factors | StaticXPath | DynamicXPath |
Definition | StaticXPath has fixed paths regardless of the web page state or content; their attributes do not change. | DynamicXPath allows changes and is flexible towards them. It also accommodates variations in elements and their positions. |
Characteristics | Directly specifies the full path from the root to the target element. | Targets elements based on their patterns and relative relationships. |
Example | //a[text()=’Login’] | //input[contains(@id, ‘username’)] |
Usage | Simple and easy to implement but cannot handle changes in elements. | Adaptable to changes but is complex and requires more consideration for implementation. |
What Are the Types of XPath?
Based on its structure and use in locating elements within XML or HTML documents, XPath
can be classified into various types. Understanding the types of XPaths helps you select the right tool for the right job. Primarily, XPath can be classified into two types:
AbsoluteXPath
An absolute XPath provides the full path to an element or target element starting from the root. It traverses through every element present in the tree.
Here are the main points about AbsoluteXPath:
- It always starts with a single forward slash (/) representing the DOM tree’s root node.
- It provides the full and unambiguous path from the root node to the targeted element.
- A forward slash (/) separates every step in the path.
Example:
ThisXPath script selects the <h1> element, starting from the root (HTML) and navigating through every child element.
Relative XPath
A relative XPath provides the path from the current node to the target element. It is more flexible regarding changes in the document structure.
Some important points about RelativeXPath:
- It is more flexible and robust than AbsoluteXPath because the relative positioning between nodes may remain the same even if the DOM structure changes.
- It allows you to locate elements based on their relationship to other known elements instead of specifying the full path.
- The common axes used are ‘/’ and ‘//’, and relationships like parent::, child::, following-sibling::, etc.
- Relative paths can be shortened using idioms like //div[@id=’x’] to quickly reach the desired element.
- They are generally preferred over absolute paths as they are less brittle to code changes and element repositioning.
Example:
ThisXPath selects the <h1> element that is a child of any <div> with the class content.
XPath Axes
XPath Axes are one of the most used features that help users navigate through the XML or HTML document to search multiple nodes from the context of the current node. They specify a relationship between the context node and the nodes to be selected and help navigate the nodes in several directions, such as parent, child, sibling, ancestor, or descendant nodes.
The primary XPath Axes are:
Axis | Syntax | Example |
Child Axis | child::node | //div[@id=’content’]/child::p |
Parent Axis | parent::node | //input[@id=’username’]/parent::div |
Ancestor Axis | ancestor::node | //span[@class=’highlight’]/ancestor::div |
Ancestor-or-self Axis | ancestor-or-self::node | //span[@class=’highlight’]/ancestor-or-self::div |
Descendant Axis | descendant::node | //div[@id=’content’]/descendant::span |
Descendant-or-self Axis | descendant-or-self::node | //div[@id=’content’]/descendant-or-self::span |
Following Axis | following::node | //h1[text()=’Title’]/following::p |
Following-sibling Axis | following-sibling::node | //h1[text()=’Title’]/following-sibling::p |
Preceding Axis | preceding::node | //h1[text()=’Title’]/preceding::div |
Preceding-sibling Axis | preceding-sibling::node | //h1[text()=’Title’]/preceding-sibling::p |
Self Axis | self::node | //div[@id=’content’]/self::div |
Namespace Axis | namespace::node | //namespace::*[name()=’ns’] |
Attribute Axis | attribute::node | //div[@id=’content’]/attribute::* |
XPath Functions
Xpath functions are crucial tools for improving the accuracy of expressions in locating the target elements within an XML or HTML document.
Some of the most used XPath functions are:
Function | Description | Syntax Example |
contains() | Check whether a string contains a target substring. | contains(@class, ‘highlight’) |
starts-with() | Check whether a string starts with a specific target substring. | starts-with(@id, ‘user’) |
text() | Select text nodes. | //p[text()=’Welcome’] |
normalize-space() | Removes leading and trailing spaces and replaces sequences of whitespace characters with a single space. | normalize-space(//p) |
last() | Fetches the last element in a set of nodes. | //li[last()] |
position() | Gets the position of a node in a set of nodes. | //li[position()=2] |
count() | Fetches the number of nodes in a node-set. | count(//div) |
sum() | Gets the sum of a numeric node-set. | sum(//price) |
name() | Returns the name of the node. | name(//div) |
local-name() | Gets the local part of the name of the node. | local-name(//div) |
Advanced XPath Techniques
AdvancedXPath techniques and strategies help enhance the ability to locate the target elements more accurately within the XML or HTML document. These techniques involve multiple XPath functions, axes, and expressions to create more flexible and reliable locators. Some of the advanced XPath techniques which are used often are:
- Combining Multiple Conditions:
Combining multiple conditions using logical operators such as ‘and’ and ‘or’ helps locate the target element more precisely.
- Selecting Nodes by Position:
Selecting nodes by position within a set helps deal with the repeated elements.
- Selecting Nodes by Index:
This technique suggests selecting nodes by their index, which is useful for repeated elements.
- Using Axes to Traverse the DOM:
Using XPath axes such as child, ancestor, descendant, following-sibling, and preceding-sibling allows for flexible and accurate navigation through the DOM.
- Combining Text and Attributes:
Combining text and attribute conditions can sometimes prove very powerful for locating specific elements easily.
- Handling Namespaces:
To locate an element efficiently when dealing with XML documents that use namespaces, we can define and use namespace prefixes in your XPath.
- Using Functions for Dynamic Content:
XPath functions like ‘normalise-space()’,’ substring()’, and ‘translate()’ help manage dynamic content.
- Using the ‘|’ Operator for Multiple Paths:
The ‘|’ operator comes in handy for selecting multiple paths and can also be useful for complex structures.
- Using Predicates for Filtering:
Using predicates, nodes can be filtered based on conditions, making our XPath expressions more precise and simple to understand.
Examples of XPath in Selenium
Xpath can be used in Selenium with multiple programming languages. Some of the examples are:
- JAVA
- Python
- C#
Best Practices for XPath in Selenium Testing
To use XPath in selenium efficiently, it is very important to check out some basic but crucial points to ensure that the tests are robust, maintainable, and efficient. Some of the best practices are:
- Prefer RelativeXPath over AbsoluteXPath.
- Use Descriptive and Unique Attributes.
- Utilise “contains()” for Dynamic Elements.
- Use “text()” to Locate Elements by Their Text Content.
- LeverageXPath Axes for Complex Structures.
- Combine Multiple Conditions Using and or.
- Normalise Spaces in Text Content.
- Use Indexing Sparingly.
- Test YourXPath Expressions.
- Handle Namespaces When Working with XML.
- KeepXPath Expressions Readable.
Testers can turn to platforms like LambdaTest to improve the effectiveness of XPath locators. LambdaTest is an AI-powered test execution platform that lets you run manual and automated test scenarios at scale with over 3000+ real devices, browsers, and OS combinations.
Conclusion
For selenium testing, XPath is a robust tool for identifying elements within XML or HTML documents through various axes, functions, and operators. To deal with dynamic and complex web pages, XPath provides advanced techniques like combining multiple conditions, using partial matches, and employing various axes.
As web applications continue to be updated and advanced daily, it is very important to learn XPath and its implementations to get reliable and accurate results while web scraping or automation testing.
In summary, we can conclude that the power of XPath in Selenium lies in its flexibility and precision, along with its advanced techniques for overcoming complex challenges.