Element Location Using XPath Axis
During testing we will sometimes come up to situations where developers are not following best practises for testability . We will frequently come up situations where elements doesn’t have any unique identifiable property. XPath axis comes to help in those situations. We can identify elements using various XPath Properties
List of various XPath Axis are available in https://developer.mozilla.org/en-US/docs/Web/XPath/Axes If you have well-defined properties to identify the element, use them as your locator. Please read locator strategy Using XPath and Other Parameters
Below are major one’s which we will frequently use
1. ancestor
This selects all ancestors of current node. That will include parent, grand parents etc Eg :
1
|
|
2. descendant
This selects all children of current node. That will include child, grand child etc Eg:
1
|
|
3. followingis
Th selects everything after the closing tag of current node Eg:
1
|
|
4. following-sibling
This selects all siblings after the closing tah of current node. Eg:
1
|
|
5. preceding
This selects everything prior to current node Eg:
1
|
|
- preceding-sibling
This selects all siblings prior to current node Eg:
1
|
|
7. child
This selects all children of current node
8. parent
This select parent of current node
As usual , you can always use combinations of above in your test. Statements can be constructed in the same way as we traverse the XPath axis
Last , but not least… we can also use regular expression in XPath.