Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'] 
like image 838
Tushar K Avatar asked Mar 15 '16 18:03

Tushar K


People also ask

What does double slash mean in XPath?

Double Slash “//” – Double slash is used to create Xpath with relative path i.e. the xpath would be created to start selection from anywhere within the document.

What does two slashes mean in code?

The double slash is a comment. Triple slash doesn't mean anything special, but it might be a comment that was added right before a division.

What is double backslash used for?

Two backslashes are used as a prefix to a server name (hostname). For example, \\a5\c\expenses is the path to the EXPENSES folder on the C: drive on server A5. See UNC, \\, path and forward slash.

What is the difference between slash and double slash and XPath?

Difference between “/” and “//” in XPathSingle slash is used to create absolute XPath whereas Double slash is used to create relative XPath. 2. Single slash selects an element from the root node. For example, /html will select the root HTML element.


2 Answers

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.

like image 198
Guy Avatar answered Sep 18 '22 15:09

Guy


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'.
like image 45
kjhughes Avatar answered Sep 17 '22 15:09

kjhughes