top of page

XPATH selectors

  1. Select a node by its name: nodename. Example: if the node is an h3 element, the selector is: h3.

  2. Select all the nodes that have a certain attribute: nodename<@attribute>. Example: Select all h3 elements that have a class: //h3<@class>.

  3. Select all the nodes for which a specified attribute has a specified value. Example: Select all h3 elements that have a class having the 'aClass' value: //h3<@class 'aClass'="'aClass'">.

  4. Select the nth child element of a node, having a certain element type: //node/child. Example: Select the third 'li' child of a 'ul' element: //ul/li<3>.

  5. Selecting the parent of a node: .. . Example: parent of a div element that has the class 'aClass': //div<@clas 'aClass'="'aClass'">/.. Let's exemplify some of these selections on a piece of HTML code:

<div class="firstClass">
<li>
<div class="secondClass andThirdClass" >
  <h3>
   <a href="aRandomLink”> randomText</a>
  </h3>
  <div class="fourthClass">
    <div>
      <div class="fifthClass">
        <a class="someSpecialClass" href="#" id="firstId">
        <span>Some text here</span>
        </a>
      </div>
      <span>Yet more text</span>
    </div>
   </div>
  </div>
</li>
<li>...same structure as the first li...</li><li>...same structure as the previous two lis</li>
</div>
 

To select:
 
  1. the h3 element: //h3

  2. the a element whose class attribute is someSpecialClass: //a<@class 'someSpecialClass'="'someSpecialClass'">

  3. the span element that has 'Some text here' as label: //div<@class 'fifthClass'="'fifthClass'">

  4. the span element that has 'Yet more text' as label: //div<@class 'fourthClass'="'fourthClass'">/div/span

  5. the first li element of the first div: //div<@class 'firstClass'="'firstClass'">/li

  6. the second li element of the first div: //div<@class 'firstClass'="'firstClass'">/li<2>

  7. all div elements whose class attributes contain the string 'Class': //div

  8. all div elements that have a class attribute containing the 'second' string and also a class attribute containing the 'Third' string: //div[contains(@class, 'second') and contains(@class, 'Third')

Recent Posts

See All
bottom of page