XPath Axes
What is it?
XPath axes are used to navigate the XML document structure and locate specific nodes or elements.
An axis represents a relationship between nodes, and it defines the direction and scope of the node selection.
XPath defines several axes that you can use to traverse the XML document.
When to use XPath axes?
When elements cannot be uniquely identified using attributes like id, name, or simple XPath expressions,
or when you need to specify a more complex path based on the relationships between nodes.
Here are the main 6 types of XPath axes:
Parent = refers to the immediate direct parent node.
Ancestor = includes all ancestor nodes.
Following = selects nodes that come after the current node.
Descendant = selects nodes below the current node.
Child = selects immediate direct children of the current node.
Sibling = selects nodes that share the same parent as the current node.
Let’s look at the syntax and examples for all those methods.
following::
Syntax-
Parent element's XPath + /following:: + Second element's XPath without any additional / or // within the combination.
Examples
//label[text()='Email']/following::input[1]
Above [1] means the index of input
//label[text()='Email']/following::input[@type='password']
Here we described the second element xpath
(//label[text()='Email']/following::input[@type='password'])[1]
If there are mutluple values , we could use index.
parent::
Selects the parent node of the current node.There is only one parent for each node in the XML document hierarchy. Only one method is available in this.
Syntax
Child element's XPath + /parent:: + Parent element's XPath without any additional / or // within the combination.
Locator
//label[text()='Email']/parent::div[@class='container']
child::
Selects all immediate child nodes of the current node. Only one method.
Syntax
Parent element's XPath + /child:: Child element's XPath without any additional / or // within the combination.
Locator
//div[@class='container']/child::label[text()='Email']
descendant::
Selects all descendant nodes of the current node, regardless of their depth. Descendant means children and grand children.
There are two methods related to the Descendant axis.
descendant- children and grand children.
descendant-or-self- children and grand children and including current node.
Syntax-
Parent element's XPath + /descendant:: descendant element's XPath without any additional / or // within the combination.
Locators
//div[@class='container']/descendant::h1[text()='Register']
//div[@class='container']/descendant::a
ancestor
Selects all ancestor nodes of the current node.So we can select parents and grand parents.
Ancestor axis in XPath refers to the nodes that are higher up in the XML document hierarchy compared to the current node.
These nodes can be the parent node, grandparent node, or any higher-level ancestor node.
There are two methods related to the Ancestor axis.
ancestor -Selects all ancestor nodes of the current node except the current node.
ancestor-or-self- Selects all ancestor nodes of the current node including current node.
Syntax-
Child element's XPath + /ancestor:: + Ancestor element's XPath without any additional / or // within the combination.
Locators-
//input[@name='name']/ancestor::div[@class='container']
sibling axes:
Following-sibling axis (following-sibling::): Selects all sibling nodes that appear after the current node.
In simple terms. elements that have same parent, but younger brothers.
Preceding-sibling axis (preceding-sibling::): Selects all sibling nodes that appear before the current node.
In simple terms. elements that have same parent, but elder brothers.
Sibling has two methods.
preceding-sibling axis (preceding-sibling::):
The preceding-sibling axis selects all the sibling nodes that appear before the current node.
following-sibling axis (following-sibling::):
The following-sibling axis selects all the sibling nodes that appear after the current node.
following-sibling::
Current element's XPath + /following-sibling:: + Sibling element's XPath
Locator
//label[text()='Last Name']/following-sibling::input[@name='name']
preceding-sibling axis
Syntax-
Current element's XPath + /preceding-sibling:: + Sibling element's XPath without any additional / or // within the combination.
Example
//label[text()='Last Name']/preceding-sibling::input[@name='name']
Further learning-
Watch this , highly recommonaded.