If double slash (//) is used 2 times in XPath, what does it mean?

31,127

Solution 1

A double slash "//" means any descendant node of the current node in the HTML tree which matches the locator.

A single slash "/" means a node which is a direct child of the current.

//div[@id='add']//span[@id=addone'] will match:

<div id="add">
  <div>
    <span id="addone">
  </div>
</div>

And:

<div id="add">
    <span id="addone">
</div>

//div[@id='add']/span[@id=addone'] will match only the second HTML tree.

Solution 2

Double slash (//) is the descendant-or-self axis; it is short for /descendant-or-self::node()/.

In your example XPath:

//div[@id='add']//span[@id='addone']
  • The first time // appears, it selects all div elements in the document with an id attribute value equal to 'add'.
  • The second time // appears, it selects all span elements that are descendents of each of the div elements selected previously.
  • Note that using two double slashes twice is different than just using double slash once. For example, //span[@id='addone'] would select all span elements with @id='addone' in the entire document, regardless of whether they are a descendent of a div with @id='add'.

Solution 3

If you'd have this:

<div id='add'>
   <ul>
      <li>
        <span id='add one' />
      </li>
   </ul>
</div>

Then

//div[@id='add']//span[@id='addone']

will result in the span because the second // means you look for any child relative to

div[@id='add']

that is span[@id='add one'].

If you'd use one slash

//div[@id='add']/span[@id='addone']

then of course you won't find it because then you look for a direct child and you'd have to use

//div[@id='add']/ul/li/span[@id='addone']

So the second // is very useful in avoiding extra hierarchy in your XPaths.

Share:
31,127
Tushar K
Author by

Tushar K

Updated on July 09, 2022

Comments

  • Tushar K
    Tushar K almost 2 years

    What does a double-slash used twice in an XPath selector mean?

    Suppose I'm using a path like:

    //div[@id='add']//span[@id=addone']
    
  • Tushar K
    Tushar K over 8 years
    I have one more question. On webpage, there is 'search' button , If i click on it, it will shown all results. Now condition is , if data is present , then it will shown data in row , but if data is not present, it will shown 'No element found' text on page. Could you please let me know how to handle this situation.
  • Tushar K
    Tushar K over 8 years
    I have try with if -else condition, but it is showing element not found exception. Please help me on it, how I will check condition data not found for particular element.
  • Tushar K
    Tushar K over 8 years
    I have one more question. On webpage, there is 'search' button , If i click on it, it will shown all results. Now condition is , if data is present , then it will shown data in row , but if data is not present, it will shown 'No element found' text on page. Could you please let me know how to handle this situation. I have try with if -else condition, but it is showing element not found exception. Please help me on it, how I will check condition data not found for particular element.
  • kjhughes
    kjhughes over 8 years
    You're welcome. Please accept this answer if it's helped, and ask a new question regarding the new search button query. Be sure to include a minimal reproducible example demonstrating how your XPath isn't selecting what you want. Thanks.
  • David Baak
    David Baak over 8 years
    You're welcome. Please accept this answer if it's helped, and ask a new question regarding the new search button query.
  • Tushar K
    Tushar K over 8 years
    I am using following logic: If (!=(webelement.xpath("").isDisplayed) element found else ((webelement.xpath("").isDisplayed) element not found. Here i have used if else loop to check which element is present. but if element is not present in first if condtion, it displayed "element not found error". Please suggest on it.
  • Tushar K
    Tushar K over 8 years
    I am using following logic: If (!=(webelement.xpath("").isDisplayed) element found else ((webelement.xpath("").isDisplayed) element not found. Here i have used if else loop to check which element is present. but if element is not present in first if condtion, it displayed "element not found error". Please suggest on it.
  • kjhughes
    kjhughes over 8 years
    @TusharK: You have two good answers and should accept one, then ask a new question. Do not use comments for questions; do not paste code into comments; and do not keep repeating yourself across comments between answers. Thanks.
  • John C.
    John C. over 2 years
    "// is short for /descendant-or-self::node()/. For example, //para is short for /descendant-or-self::node()/child::para and so will select any para element in the document (even a para element that is a document element will be selected by //para since the document element node is a child of the root node); div//para is short for div/descendant-or-self::node()/child::para and so will select all para descendants of div children." w3.org/TR/1999/REC-xpath-19991116
  • John C.
    John C. over 2 years
    Since this is the top answer, I recommend changing "means any descendant node of the current node". Because "//" includes self. How about "means any descendant's nodes or self's node".
  • John C.
    John C. over 2 years
    This little fact might be helpful too: In XPath when we say "-or-" it is the Set Theory "OR". It means "the union of". As opposed the Set Theory "AND" which means "intersection of". This is why "descendant-or-self" returns both self and descendants. [self] || [descendants]