☰ See All Chapters |
XPath Axes
Navigating from one element to another element in Xpath is called traversing. In order to traverse from one element to another, we use XPath axes. Axes are something using which XPath traverses in the html tree either in upward or downward direction. We have the following 6 XPath Axes in selenium.
child
descendant
parent
ancestor
preceding-sibling
following-sibling
For all the examples of all the below axes we consider the below sample html code
<html> </head> <body> <form id="form"> Input 1 : <input type="text"> Input 2 : <input type="text"> Input 3 : <input type="text"> Input 4 : <input type="text"> Input 5 : <input type="text"> Input 6 : <input type="text"> Input 7 : <input type="text"> Input 8 : <input type="text"> Input 9 : <input type="text"> </form> </body> </html> |
child (/)
Using this axes, xpath traverses to the immediate child element in the html tree. Axes is represented by single forward slash (/). We use this child axes in deriving the absolute xpath expression.
e.g.: /html → can be written using child axes as → child::html
descendant (//)
Using this axes, xpath traverses to any child element in the html tree. Axes is represented by double forward slash (//). Use this descendant axes in deriving the relative xpath expression.
e.g.: //input[5] → can be written using descendant axes as → descendant::input[5]
parent axes (/..)
Using this axes, xpath traverses to the immediate parent element in the html tree. Shortcut of this axes is single forward slash (/..)
e.g.: //input[5]/.. → can be written using parent axes as → //input[5]/parent::form
ancestor axes
Using this axes, xpath traverses to any parent element in the html tree.
e.g.: //input[5]/../.. → can be written using ancestor axes as → //descendant::input[5]/ancestor::body
preceding-sibling
Using this axes, xpath traverses with in the siblings in upward direction.
e.g.: → xpath using preceding-sibling axes → descendant::input[5]/preceding-sibling::input[1] - it will select Input 4
following-sibling
Using this axes, xpath traverses with in the siblings in downward direction.
e.g. : → xpath using following-sibling axes → descendant::input[5]/following-sibling::oinput[1] - it will select Input 6
Following table illustrates a detailed level understanding of all the xpath axes for below html code
<html> </head> <body> <form id="form"> Input 1 : <input type="text"> Input 2 : <input type="text"> Input 3 : <input type="text"> Input 4 : <input type="text"> Input 5 : <input type="text"> Input 6 : <input type="text"> Input 7 : <input type="text"> Input 8 : <input type="text"> Input 9 : <input type="text"> </form> </body> </html> |
Axes | XPath using axes | XPath without using axes | Matching element |
child | //form[@id='form']/child::input[9] | //form[@id='form']/input[1] | Input 9 |
descendant | //descendant::input[5] | // input[5] | Input 5 |
parent | //descendant::input[5]/parent::form | //input[4]/.. | form tag |
//input[5]/parent::form | Not available |
| |
ancestor | //input[5]/ ancestor::form | form tag | |
//input[5]/ ancestor::body | body tag | ||
//input[5]/ ancestor::hmtl | html tag | ||
preceding-sibling | //input[5]/preceding-sibling::input | Not available | Input 1 (It matches with Input 1, Input 2, Input 3, Input 3, but selenium return first matching element, i.e. Input 1) |
//input[5]/preceding-sibling::input[1] | Input 4 | ||
//input[5]/preceding-sibling::input[2] | Input 3 | ||
//input[5]/preceding-sibling::input[3] | Input 2 | ||
//input[5]/preceding-sibling::input[last()] | Input 1 | ||
//input[5]/preceding-sibling::input[last()-1] | Input 2 | ||
//input[5]/preceding-sibling::input[position()=2] | Input 3 | ||
following-sibling | //input[5]/following-sibling::input | Not available | Input 1 (It matches with Input 6, Input 6, Input 7, Input 8, but selenium return first matching element, i.e. Input 6) |
//input[5]/following-sibling::input[1] | Input 6 | ||
//input[5]/following-sibling::input[2] | Input 7 | ||
//input[5]/following-sibling::input[3] | Input 8 | ||
//input[5]/following-sibling::input[position()=2] | Input 7 | ||
//input[5]/following-sibling::input[last()-1] | Input 8 | ||
//input[5]/following-sibling::input[position()=last()] | Input 9 |
You can write the script and test these using our Test Page
All Chapters