Xpath Cheatsheet and Refcard
Xpath Cheatsheet and Refcard
Testing
Browser console
$x("//div")
Selectors
Descendant selectors
CSS Xpath ?
h1 //h1 ?
div p //div//p ?
ul > li //ul/li ?
1/8
CSS Xpath ?
:root / ?
Attribute selectors
CSS Xpath ?
#id //*[@id="id"] ?
input[type="submit"] //input[@type="submit"]
a#abc[for="xyz"] //a[@id="abc"][@for="xyz"] ?
a[rel] //a[@rel]
Order selectors
CSS Xpath ?
li#id:first-of-type //li[1][@id="id"] ?
a:first-child //*[1][name()="a"]
a:last-child //*[last()][name()="a"]
Siblings
CSS Xpath ?
2/8
CSS Xpath ?
h1 ~ ul //h1/following-sibling::ul ?
h1 + ul //h1/following-sibling::ul[1]
h1 ~ #id //h1/following-sibling::[@id="id"]
jQuery
CSS Xpath ?
$('li').closest('section') //li/ancestor-or-self::section
$('a').attr('href') //a/@href ?
$('span').text() //span/text()
Other things
CSS Xpath ?
h1:not([id]) //h1[not(@id)] ?
Class check
Xpath doesn’t have the “check if part of space-separated list” operator, so this is the
workaround (source).
Expressions
3/8
// ul / a[@id='link']
Prefixes
// //hr[@class='edge'] Anywhere
./ ./a Relative
/ /html/body/div Root
Axes
/ //ul/li/a Child
// //[@id="list"]//a Descendant
Separate your steps with /. Use two (//) if you don’t want to select direct children.
Steps
//div
//div[@name='box']
//[@id='link']
A step may have an element name (div) and predicates ([...]). Both are optional. They
can also be these other things:
Predicates
Predicates
//div[true()]
//div[@class="head"]
//div[@class="head"][@id="top"]
4/8
Operators
# Comparison
//a[@id = "xyz"]
//a[@id != "xyz"]
//a[@price > 25]
# Logic (and/or)
//div[@id="head" and position()=2]
//div[(x and y) or not(z)]
Using nodes
Indexing
Chaining order
a[1][@href='/']
a[@href='/'][1]
Nesting predicates
//section[.//h1[@id='hi']]
Functions
Node functions
5/8
name() # //[starts-with(name(), 'h')]
text() # //button[text()="Submit"]
# //button/text()
lang(str)
namespace-uri()
count() # //table[count(tr)=1]
position() # //ol/li[position()=2]
Boolean functions
not(expr) # button[not(starts-with(text(),"Submit"))]
String functions
contains() # font[contains(@class,"head")]
starts-with() # font[starts-with(@class,"head")]
ends-with() # font[ends-with(@class,"head")]
concat(x,y)
substring(str, start, len)
substring-before("01/02", "/") #=> 01
substring-after("01/02", "/") #=> 02
translate()
normalize-space()
string-length()
Type conversion
string()
number()
boolean()
Axes
Using axes
//ul/li # ul > li
//ul/child::li # ul > li (same)
//ul/following-sibling::li # ul ~ li
//ul/descendant-or-self::li # ul li
//ul/ancestor-or-self::li # $('ul').closest('li')
Steps of an expression are separated by /, usually used to pick child nodes. That’s not
always true: you can specify a different “axis” with ::.
// ul /child:: li
Child axis
6/8
# both the same
//ul/li/a
//child::ul/child::li/child::a
Descendant-or-self axis
Other axes
ancestor
ancestor-or-self
descendant
namespace
following
following-sibling
preceding
7/8
Axis Abbrev Notes
preceding-sibling
Unions
//a | //span
More examples
Examples
Find a parent
//section[h1[@id='section-name']]
//section[//h1[@id='section-name']]
Closest
./ancestor-or-self::[@class="box"]
Attributes
8/8