---
title: XPath
date: 2020-12-19 22:15:43
background: bg-[#77aeeb]
tags:
- document
- expression
- select
categories:
- Toolkit
intro: |
This is an [XPath](https://en.wikipedia.org/wiki/XPath) selectors cheat sheet, which lists commonly used XPath positioning methods and CSS selectors
plugins:
- copyCode
---
## XPath Selectors {.cols-6}
### Getting started {.col-span-2}
- [Xpath test bed](http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm) _(whitebeam.org)_
Test in Firefox or Chromium based browser console:
```console
$x('/html/body')
$x('//h1')
$x('//h1')[0].innerText
$x('//a[text()="XPath"]')[0].click()
```
### Descendant selectors {.col-span-2}
| Xpath | CSS |
| ------------ | ------------ |
| `//h1` | h1 |
| `//div//p` | div p |
| `//ul/li` | ul > li |
| `//ul/li/a` | ul > li > a |
| `//div/*` | div > \* |
| `/` | :root |
| `/html/body` | :root > body |
{.show-header}
### Order selectors {.col-span-2}
| Xpath | CSS |
| ------------------- | -------------------- |
| `//ul/li[1]` | ul > li:first-child |
| `//ul/li[2]` | ul > li:nth-child(2) |
| `//ul/li[last()]` | ul > li:last-child |
| `//li[@id="id"][1]` | li#id:first-child |
| `//a[1]` | a:first-child |
| `//a[last()]` | a:last-child |
{.show-header}
### Attribute selectors {.col-span-3 .row-span-2}
| Xpath | CSS |
| ------------------------------- | -------------------- |
| `//*[@id="id"]` | #id |
| `//*[@class="class"]` | .class |
| `//input[@type="submit"]` | input[type="submit"] |
| `//a[@id="abc"][@for="xyz"]` | a#abc[for="xyz"] |
| `//a[@rel]` | a[rel] |
| `//a[starts-with(@href, '/')]` | a[href^='/'] |
| `//a[ends-with(@href, '.pdf')]` | a[href$='pdf'] |
| `//a[contains(@href, '://')]` | a[href*='`:`//'] |
| `//a[contains(@rel, 'help')]` | a[rel~='help'] |
{.show-header}
### Siblings {.col-span-3}
| Xpath | CSS |
| ------------------------------------ | -------- |
| `//h1/following-sibling::ul` | h1 ~ ul |
| `//h1/following-sibling::ul[1]` | h1 + ul |
| `//h1/following-sibling::[@id="id"]` | h1 ~ #id |
{.show-header}
### jQuery {.col-span-3}
| Xpath | CSS |
| -------------------------------- | -------------------------- |
| `//ul/li/..` | $('ul > li').parent() |
| `//li/ancestor-or-self::section` | $('li').closest('section') |
| `//a/@href` | $('a').attr('href') |
| `//span/text()` | $('span').text() |
{.show-header}
### Misc selectors {.col-span-3}
| Xpath | CSS |
| --------------------------------- | ------------------------- | --------------------- |
| `//h1[not(@id)]` | h1:not([id]) |
| `//button[text()="Submit"]` | Text match |
| `//button[contains(text(),"Go")]` | Text contains (substring) |
| `//product[@price > 2.50]` | Arithmetic |
| `//ul[*]` | Has children |
| `//ul[li]` | Has children (specific) |
| `//a[@name or @href]` | Or logic |
| `//a | //div` | Union (joins results) |
{.show-header}
## XPath Expressions
### Steps and axes {.secondary}
| - | - | - | - |
| ---- | ---- | ---- | --------------- |
| `//` | `ul` | `/` | `a[@id='link']` |
| Axis | Step | Axis | Step |
{.left-text}
### Prefixes
| Prefix | Example | Means |
| ------ | --------------------- | -------- |
| `//` | `//hr[@class='edge']` | Anywhere |
| `/` | `/html/body/div` | Root |
| `./` | `./div/p` | Relative |
{.show-header}
### Axes
| Axis | Example | Means |
| ---- | ------------------- | ---------- |
| `/` | `//ul/li/a` | Child |
| `//` | `//[@id="list"]//a` | Descendant |
{.show-header}
## XPath Predicates
### Predicates
```bash
//div[true()]
//div[@class="head"]
//div[@class="head"][@id="top"]
```
Restricts a nodeset only if some condition is true. They can be chained.
### Operators
```bash
# Comparison
//a[@id = "xyz"]
//a[@id != "xyz"]
//a[@price > 25]
```
```bash
# Logic (and/or)
//div[@id="head" and position()=2]
//div[(x and y) or not(z)]
```
### Using nodes
```bash
# Use them inside functions
//ul[count(li) > 2]
//ul[count(li[@class='hide']) > 0]
```
```bash
# Returns `