0% found this document useful (0 votes)
19 views

Xpath cheatsheet

Uploaded by

cokegplays
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Xpath cheatsheet

Uploaded by

cokegplays
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

10/1/25, 12:07 Xpath cheatsheet 10/1/25, 12:07 Xpath cheatsheet

DEVHINTS.IO Edit Descendant selectors

h1 //h1 ?

Xpath cheatsheet div p //div//p ?

ul > li //ul/li ?

Your new development career ul > li > a //ul/li/a

awaits. Check out the latest listings.


div > * //div/*
ads via Carbon
:root / ?

:root > body /body

Xpath test bed

Test queries in the Xpath test bed: Attribute selectors

#id //*[@id="id"] ?
Xpath test bed
(whitebeam.org)
.class //*[@class="class"] …kinda

input[type="submit"] //input[@type="submit"]

Browser console a#abc[for="xyz"] //a[@id="abc"][@for="xyz"] ?

$x("//div") a[rel] //a[@rel]

a[href^='/'] //a[starts-with(@href, '/')] ?


Works in Firefox and Chrome.
a[href$='pdf'] //a[ends-with(@href, '.pdf')]

a[href*='://'] //a[contains(@href, '://')]

//a[contains(@rel, 'help')] …kinda


Selectors
a[rel~='help']

https://devhints.io/xpath 1/14 https://devhints.io/xpath 2/14


10/1/25, 12:07 Xpath cheatsheet 10/1/25, 12:07 Xpath cheatsheet

Order selectors Other things

ul > li:first-of-type //ul/li[1] ? h1:not([id]) //h1[not(@id)] ?

ul > li:nth-of-type(2) //ul/li[2] Text match //button[text()="Submit"] ?

ul > li:last-of-type //ul/li[last()] Text match (substring) //button[contains(text(),"Go")]

li#id:first-of-type //li[1][@id="id"] ? Arithmetic //product[@price > 2.50]

a:first-child //*[1][name()="a"] Has children //ul[*]

a:last-child //*[last()][name()="a"] Has children (specific) //ul[li]

Or logic //a[@name or @href] ?

Union (joins results) //a | //div ?


Siblings

h1 ~ ul //h1/following-sibling::ul ?

h1 + ul //h1/following-sibling::ul[1] Class check

h1 ~ #id //h1/following-sibling::[@id="id"] //div[contains(concat(' ',normalize-space(@class),' '),' foobar ')]

Xpath doesn’t have the “check if part of space-separated list” operator, so this is the workaround (source).

jQuery

$('ul > li').parent() //ul/li/.. ?

$('li').closest('section') //li/ancestor-or-self::section Expressions


?
$('a').attr('href') //a/@href
Steps and axes
$('span').text() //span/text()
// ul / a[@id='link']

Axis Step Axis Step

https://devhints.io/xpath 3/14 https://devhints.io/xpath 4/14


10/1/25, 12:07 Xpath cheatsheet 10/1/25, 12:07 Xpath cheatsheet

Prefixes Predicates

Prefix Example What //div[true()]


//div[@class="head"]
// //hr[@class='edge'] Anywhere //div[@class="head"][@id="top"]

./ ./a Relative
Restricts a nodeset only if some condition is true. They can be chained.
/ /html/body/div Root

Begin your expression with any of these.


Operators

# Comparison

Axes //a[@id = "xyz"]


//a[@id != "xyz"]
//a[@price > 25]
Axis Example What

/ //ul/li/a Child # Logic (and/or)


//div[@id="head" and position()=2]
// //[@id="list"]//a Descendant //div[(x and y) or not(z)]

Separate your steps with /. Use two (//) if you don’t want to select direct children. Use comparison and logic operators to make conditionals.

Steps Using nodes

//div # Use them inside functions


//div[@name='box'] //ul[count(li) > 2]
//[@id='link'] //ul[count(li[@class='hide']) > 0]

# This returns `<ul>` that has a `<li>` child


A step may have an element name (div) and predicates ([...]). Both are optional. They can also be
//ul[li]
these other things:

//a/text() #=> "Go home" You can use nodes inside predicates.
//a/@href #=> "index.html"
//a/* #=> All a's child elements

Predicates

https://devhints.io/xpath 5/14 https://devhints.io/xpath 6/14


10/1/25, 12:07 Xpath cheatsheet 10/1/25, 12:07 Xpath cheatsheet

Indexing Node functions

//a[1] # first <a> name() # //[starts-with(name(), 'h')]


//a[last()] # last <a> text() # //button[text()="Submit"]
//ol/li[2] # second <li> # //button/text()
//ol/li[position()=2] # same as above lang(str)
//ol/li[position()>1] # :not(:first-of-type) namespace-uri()

Use [] with a number, or last() or position(). count() # //table[count(tr)=1]


position() # //ol/li[position()=2]

Chaining order
Boolean functions

a[1][@href='/']
not(expr) # button[not(starts-with(text(),"Submit"))]
a[@href='/'][1]

Order is significant, these two are different.

String functions

contains() # font[contains(@class,"head")]
Nesting predicates starts-with() # font[starts-with(@class,"head")]
ends-with() # font[ends-with(@class,"head")]
//section[.//h1[@id='hi']]
concat(x,y)
substring(str, start, len)
This returns <section> if it has an <h1> descendant with id='hi'.
substring-before("01/02", "/") #=> 01
substring-after("01/02", "/") #=> 02
translate()
normalize-space()
string-length()

Functions

Type conversion

string()
number()
boolean()

Axes
https://devhints.io/xpath 7/14 https://devhints.io/xpath 8/14
10/1/25, 12:07 Xpath cheatsheet 10/1/25, 12:07 Xpath cheatsheet

Using axes Descendant-or-self axis

//ul/li # ul > li # both the same


//ul/child::li # ul > li (same) //div//h4
//ul/following-sibling::li # ul ~ li //div/descendant-or-self::h4
//ul/descendant-or-self::li # ul li
//ul/ancestor-or-self::li # $('ul').closest('li')
// is short for the descendant-or-self:: axis.

Steps of an expression are separated by /, usually used to pick child nodes. That’s not always true: you
# both the same
can specify a different “axis” with ::.
//ul//[last()]
//ul/descendant-or-self::[last()]
// ul /child:: li

Axis Step Axis Step

Child axis

# both the same


//ul/li/a
//child::ul/child::li/child::a

child:: is the default axis. This makes //a/b/c work.

# both the same


# this works because `child::li` is truthy, so the predicate succeeds
//ul[li]
//ul[child::li]

# both the same


//ul[count(li) > 2]
//ul[count(child::li) > 2]

https://devhints.io/xpath 9/14 https://devhints.io/xpath 10/14


10/1/25, 12:07 Xpath cheatsheet 10/1/25, 12:07 Xpath cheatsheet

Other axes Examples

Axis Abbrev Notes //* # all elements


count(//*) # count all elements
ancestor (//h1)[1]/text() # text of the first h1 heading
//li[span] # find a <li> with an <span> inside it
ancestor-or-self # ...expands to //li[child::span]
//ul/li/.. # use .. to select a parent
attribute @ @href is short for attribute::href

child div is short for child::div

descendant Find a parent

descendant-or-self // // is short for /descendant-or-self::node()/ //section[h1[@id='section-name']]

namespace
Finds a <section> that directly contains h1#section-name
self . . is short for self::node()
//section[//h1[@id='section-name']]
parent .. .. is short for parent::node()

following Finds a <section> that contains h1#section-name. (Same as above, but uses descendant-or-self instead
of child)
following-sibling

preceding

preceding-sibling Closest

./ancestor-or-self::[@class="box"]
There are other axes you can use.

Works like jQuery’s $().closest('.box').

Unions

//a | //span Attributes

Use | to join two expressions. //item[@price > 2*@discount]

Finds <item> and check its attributes

More examples
References
https://devhints.io/xpath 11/14 https://devhints.io/xpath 12/14
10/1/25, 12:07 Xpath cheatsheet 10/1/25, 12:07 Xpath cheatsheet

Xpath test bed (whitebeam.org) Other HTML cheatsheets Top cheatsheets

Input tag HTML meta tags Elixir ES2015+


cheatsheet cheatsheet cheatsheet cheatsheet

Layout thrashing Appcache React.js Vim


cheatsheet cheatsheet cheatsheet cheatsheet

Applinks HTML Vimdiff Vim scripting


cheatsheet cheatsheet cheatsheet cheatsheet
0 Comments for this cheatsheet. Write yours!

Search 357+ cheatsheets

Over 357 curated cheatsheets, by developers for developers.

Devhints home

https://devhints.io/xpath 13/14 https://devhints.io/xpath 14/14

You might also like