Developer Snippet Diary

Xpath all commands tutorial

/ single slash is used for direct child, ie /html/body/div/header

// double is used for find anywhere ie //header

 

1. Selecting nodes
Command: Selects all 'div' nodes in the document.

//div

2. Selecting nodes with specific attribute
Command: //[@attribute='value']  //Selects all 'a' nodes with the class attribute value 'example'.

<a class="example">hello </a>
//a[@class='example']

<div class="loepr" data-id="review-badge-date">hello </div>
//div[@data-id='review-badge-date']

3. Selecting nodes with any attribute
Command: //nodename[@attribute] //Selects all 'img' nodes with a 'src' attribute.

//img[@src]

4. Selecting nodes with specific text
//nodename[text()='text']  //Selects all 'p' nodes with the exact text 'Hello, world

 //p[text()='Hello, world!'] 

5. Direct child : Absolute path. only work if the document structure exactly matches that hierarchy.

/html/body/div/header

Go to html>body>div>header

6.NthElement : Select Direct first child or second

/html/body/div[1]
/html/body/div[2]

7. Selecting text :: Text select of an element

/html/body/div/header[1]/text()



 

Cheat sheet

 

Symbol / Expression Meaning Example Result
/ Absolute path (root → direct child) /html/body/div Selects the <div> that is a direct child of <body>
// Anywhere (descendant, any depth) //div Selects all <div> elements in the document
. Current node (self) .//p From current node, select all <p> descendants
.. Parent of current node ../h2 From current node, go up one level and select <h2>
@ Attribute selector //a/@href Selects the href attribute values of all <a>
[@attr='value'] Filter by attribute value //div[@id='main'] Selects <div id="main">
contains(@attr,'text') Partial attribute match //div[contains(@class,'header')] Matches <div class="site-header">
[n] Nth element (1-based) (//p)[1] Selects the first <p> in the document
text() Node text //h1/text() Extracts the text inside <h1>
* Wildcard (any element) //div/* All direct children of <div>
Posted by: R GONDAL
Email: rizikmw@gmail.com