Some Beautiful Touches with Xpath Axes for Selenium

Sadik Erdem Sen
Kariyer.net Tech
Published in
4 min readJul 7, 2021

For software test automation scripting, you need some instruments to write your automation tests that; test automation framework, test framework, project management/builder tool, a development language, an ide, and way to catch elements on front-end.

And here come is the protagonist of this story that ‘Xpath’ which one of the ways to catch elements on front-end.

Xpath

XPath is a technique in Selenium to navigate through the HTML structure of a page. XPath enables testers to navigate through the XML structure of any document, and this can be used on both HTML and XML documents.

Sometimes it’s hard to reach elements which you want and xpath axes helps you on that point.

Xpath Axes

First of all lets add our Dom Structure and try to explain xpath axes on https://www.kariyer.net/aday/giris internet address.

1. ancestor

if i had used just ancestor axe,

//input[@id=’username’]/ancestor::div

the element would be chosen all parent’s and grandparent’s div tag of the current node(//input[@id=’username’]);

2. ancestor-or-self

If i had add -or-self near the ancestor axe;

//input[@id=’username’]/ancestor-or-self::div

the element would be chosen both all parent’s and grandparent’s div tag of the current node and the current node itself.

3. attribute

If you just need to reach element with it’s specified attribute and eliminate the other matches, you use attribute axe;

//input/attribute::placeholder

this axe will bring you the unique result of your search.

4. child

Selects all(* is the key chracter to select all) elements children of the current node;

//div[@class=’login-page-container’]/child::*

if you want to select just h3 tag, you must write the xpath like that;

//div[@class=’login-page-container’]/child::h3

5. descendant

Selects all descendants (children, grandchildren, etc.) of the current node

//div[@class=’login-page-container’]/descendant::*

6. descendant-or-self

Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself.

//div[@class=’login-page-container’]/descendant-or-self//*

7. following

Selects everything in the document after the closing tag of the current node.

//div[@class=’login-page-container’]/following::*

8. following-sibling

Selects siblings after the current node.

//div[@class=’header-wrapper’]/following-sibling::div

9. namespace

Selects all namespace nodes of the current node.

If the namespace axis is implemented (Firefox/Mozilla refuses to do that) then //namespace::node() gives you all namespace nodes. But note that namespace nodes in the XSLT/XPath data model are different from namespace declaration attributes in the XML document.

Also be mentioned that; you can use namespace:: axis in XPath 1.0. But in 2.0 it’s deprecated and only works in compatibility mode.

10. parent

Selects the parent of the current node.

//input[@id=’username’]/parent::div

11. preceding

The preceding axis contains all nodes that are before the context node in document order, excluding any ancestors of the context node, and also excluding attribute nodes and namespace nodes.

//input[@id=’username’]/preceding::*

12. preceding-sibling

Selects all siblings before the current node.

//label[@for=’rememberMe’]/preceding-sibling::*

13. self

The self axis contains just the context node, and you can abbreviate “self::node()” as “.”. This is a useful axis to know about, because as you know, if you omit the axis, the default is child::, but sometimes you want to refer to the current node instead. For example, //self::label is true only if the context node is a <label> element.

//self::label

conclusion

Xpath axes are the main column of this building. Catching the right element on front-end has vital importance when you automating your tests. Otherwise you will be getting a lot of ‘element not found’ errors that you will have to trace.

references

--

--