Understanding XPath and Utilizing XPath Axes in Selenium Automation

XPath is a powerful and widely-used language for locating elements in XML and HTML documents. When working with Selenium, an automation framework, XPath can be utilized to navigate through the document structure and efficiently identify elements for automated testing and web scraping purposes. In this article, we will delve into the fundamentals of XPath and explore the various XPath axes, providing practical examples to demonstrate their usage within Selenium.

Exploring XPath and its Different Types

XPath:

XPath is an acronym for XML path language. XML uses non – XML syntax to traverse through XML and HTML based documents to uniquely identify different elements or different parts of the document. It was developed by the World Wide Web  Consortium. In selenium if we are unable to find a element using locators such as className , id , tagName etc xpath is used to identify them.XPath expressions can be constructed based on element names, attributes, relationships, and more. XPath’s can be classified into two different types broadly Absolute XPath and Relative XPath.

Absolute XPath:

Absolute XPath is type of XPath in which the path starts from the root element and goes on till the desired element which we want to identify uniquely. The absolute XPath starts with ‘/’. There is one major drawback with absolute XPath. If there is any change in the DOM structure like addition or removal of a web element the constructed XPath breaks and will not be able to identify the web element further.

Relative XPath:

Relative XPath is type of XPath in which the path starts current node or the selected node unlike starting from the root node in absolute XPath. The relative XPath starts with ‘//’ This kind of XPath is ideally used in creating automation test scripts. The reason behind using relative rather than absolute is that even though there is change in elements in DOM there won’t be any impact for Relative XPath’s.

Element Identification Using XPath

XPath offers various methods for locating elements within a document. One frequently employed technique involves using element tag names to select elements based on their HTML or XML tags. Moreover, XPath enables the filtering of elements by attributes like class, id, or name, facilitating accurate identification even in intricate document structures.

XPath Axes

Usually the Absolute or Relative XPath is used to identify a desired web element uniquely in real there are some scenarios where some elements are either dynamic or too complex to locate using the XPath . In Such scenarios XPath axes are used along with XPath methods to identify the elements. The XPath axis gives us information about on which axis the element lies relative to another element.Axes provide a way to traverse elements based on their relationships with other elements. Let’s explore some of the most commonly used axes:

Types of XPath Axes

Let’s have an overview on different types of XPath axes available to us with practical examples and illustrations. There are 13 different types of XPath axes as described below:

Child Axis : It contains or selects all the different child nodes available in the current context.

Parent Axis: It contains or selects parent node of the current node. If the current node is root node then there will not be any parent node. For rest of the nodes there will be one parent node.

Self Axis: The self axis selects the current node itself. It consistently identifies a single node that represents the self-element.

Ancestor Axis: The ancestor axis captures all ancestor elements (parent, grandparent, great-grandparents, etc.) of the current node. This axis invariably includes the root node, except when the current node itself is the root node.

Ancestor-or-self Axis: The ancestor-or-self axis encompasses both the current node itself and all its ancestor elements (parent, grandparent, great-grandparents, etc.).

Descendant Axis: The descendant axis retrieves all descendant elements (children, grandchildren, etc.) of the current node.

Descendant-or-self Axis: The descendant-or-self axis includes both the current node itself and all its descendant elements (children, grandchildren, etc.) when selected.

Following Axis: The following axis captures all elements (nodes) in the document that appear after the closing tag of the current node.

Following-sibling Axis: The following-sibling axis selects all sibling nodes that appear after the current node at the same level. In other words, it identifies the element that comes after the current node.

Preceding Axis: The preceding axis retrieves all nodes that appear before the current node in the document, excluding ancestor, attribute, and namespace nodes.

Preceding-sibling Axis: The preceding-sibling axis chooses all sibling nodes that come before the current node.

Attribute Axis: The attribute axis selects the element node based on the attribute identifier (@) of the current node. If the current node is not an element node, this axis will be empty. Both the expressions attribute::type and @type are equivalent and can be used interchangeably.

Namespace Axis: The namespace axis, which is one of the 13 XPath axes, is used to select all namespace nodes associated with the current node. However, if the current node is not an element node, this axis will be empty.

Here are examples for Some of the XPath axes we can similarly write down for the rest of them :

 

Conclusion

XPath is a versatile tool that enhances Selenium automation by enabling precise element identification and navigation within XML and HTML documents. Understanding XPath basics and familiarizing oneself with the various XPath axes empower testers and developers to write robust automation scripts for web testing and data extraction scenarios. By incorporating XPath effectively, Selenium users can optimize their automation efforts and improve overall testing efficiency.

Leave a Comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Shares
Share
Tweet
Pin