Difference between Single Slash ' / ' and Double Slash ' // ' in XPath is a very simple yet very important interview question for an automation engineer. In this article, we will learn the difference in a very brief manner so that we can understand the topic clearly.
Single Slash ' / ':
Single Slash ' / ' starts selection from the root node. It is the syntax for '/child::node()' which means select all the nodes that are the child of the matching nodes. It is mainly used to construct Absolute XPath.
For HTML pages, an Absolute XPath is given below:
/html/body/div
We may create an XPath like below:
/body/div
But the above expression will not be able to find any result as 'body' is not the root node.
Double Slash ' // ':
Double Slash ' // ' starts selection from the document where it finds the matching nodes. It is the syntax for '/descendant-or-self::node()' which means select all the nodes that are descendants of the matching nodes including the matching nodes themselves. It is used to construct Relative XPath.
For HTML pages, a Relative XPath is given below:
//div
Above XPath will find all the 'div' element from the document
Single Slash ' / ' Before Double Slash ' // ':
Single Slash ' / ' must start from root node. So the XPath having Single Slash ' / ' before Double Slash ' // ' will always start with '/html' for HTML documents.
/html//div
Above XPath is correct but adding '/html' do not provide any significance cause '/html//div' will return the same results as '//div'.
Double Slash ' // ' Before Single Slash ' / ':
Double Slash ' // ' only selects all the nodes that are descendants of the matching nodes including the matching nodes themselves. Single Slash ' / ' only selects all the nodes that are the child of the matching nodes.
XPath having Double Slash ' // ' before Single Slash ' / ' is given below:
//div/span
For Example:
<div id='div_one'> <span id='span_one' /> <div id='div_two'> <span id='span_two' /> </div> </div>
Here, If we use '//div/span' expression then we will get two 'span' nodes.
Why?
- For '//div' we will get two div nodes (div_one & div_two) because Double Slash ' // ' only selects all the nodes that are descendants of the matching nodes including the matching nodes themselves.
- For '/span' we will get two span nodes (span_one & span_two) as span_one is the direct child of div_one & span_two is the direct child of div_two.
Another Example:
<div id='div_one'> <ul> <li> <span id='span_one' /> </li> </ul> </div> <div id='div_two'> <p> <span id='span_two' /> </p> </div>
Here, If we use '//div/span' expression then we will not be able to get any result as 'span' is not the direct child of any of the above 'div' nodes.
Double Slash ' // ' After Double Slash ' // ':
Double Slash '
// ' only selects all the nodes that are descendants of the matching
nodes including the matching nodes themselves.
XPath having Double Slash ' // ' after Double Slash ' // ' is given below:
//div//span
For Example:
<div id='div_one'> <span id='span_one' /> <div id='div_two'> <span id='span_two' /> </div> </div>
<div id='div_three'> <ul> <li> <span id='span_three' /> </li> </ul> </div> <div id='div_four'> <p> <span id='span_four' /> </p> </div>
Here, If we use '//div//span' XPath then we will get four 'span' nodes.
Why?
- For '//div' we will get four div node (div_one, div_two, div_three & div_four) because Double Slash ' // ' selects all the nodes that are descendants of the matching nodes including the matching nodes themselves.
- For '//span' we will get four span node (span_one, span_two, span_three & span_four) as span_one, span_two, span_three & span_four are the descendants of each of the div nodes (div_one, div_two, div_three & div_four) which were selected previously (by '//div').
Hopefully, the above explanation helped you to understand the difference between Single Slash ' / ' and Double Slash ' // ' in XPath. Please let me know in the comment section below if you find anything difficult to understand. I'll try my level best to help you.
No comments:
Post a Comment