Xpath Cheatsheet 1568817897 PDF
Xpath Cheatsheet 1568817897 PDF
Xpath Cheatsheet 1568817897 PDF
Xpath
Descendant selectors
h1 //h1 ?
div p //div//p ?
ul > li //ul/li ?
:root / ?
Attribute selectors
#id //[@id="id"] ?
input[type="submit"] //input[@type="submit"]
a#abc[for="xyz"] //a[@id="abc"][@for="xyz"] ?
a[rel] //a[@rel]
Order selectors
a: rst-child //a[1]
a:last-child //a[last()]
Siblings
h1 ~ ul //h1/following-sibling::ul ?
h1 + ul //h1/following-sibling::ul[1]
h1 ~ #id //h1/following-sibling::[@id="id"]
jQuery
$('li').closest('section') //li/ancestor-or-self::section
$('a').attr('href') //a/@href ?
$('span').text() //span/text()
Other things
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):
//div[contains(concat(' ',normalize-space(@class),' '),' foobar ')]
Expressions
Pre xes
Begin your expression with any of these.
// anywhere //hr[@class='edge']
./ relative ./a
/ root /html/body/div
Axes
Separate your steps with /. Use two (//) if you don’t want to select direct children.
/ child //ul/li/a
// descendant //[@id="list"]//a
Steps
A step may have an element name ( div) and predicates ([...]). Both are optional.
//div
//div[@name='box']
//[@id='link']
Predicat
Predicates ([...])
Restricts a nodeset only if some condition is true. They can be chained.
//div[true()]
//div[@class="head"]
//div[@class="head"][@id="top"]
Operators
Use comparison and logic operators to make conditionals.
# 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
You can use nodes inside predicates.
Indexing
Use []with a number, or last()or position().
Chaining order
Order is significant, these two are different.
a[1][@href='/']
a[@href='/'][1]
a[@href='/'][1]
Nesting predicates
This returns <section>if it has an <h1>descendant with id='hi'.
//section[//h1[@id='hi']]
Fun ions
Node functions
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()
Ax
Using axes
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/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')
Child axis
This is the default axis. This makes //a/b/c work.
Descendant-or-self axis
//is short for the descendant-or-self:: axis.
Other axes
There are other axes you can use.
ancestor
ancestor-or-self
descendant
namespace
following
following-sibling
preceding
preceding-sibling
Unions
Use |to join two expressions.
//a | //span
More exampl
//* # all elements
count(//*) # count all elements
(//h1)[1]/text() # text of the rst h1 heading
//li[span] # nd a <li> with an <span> inside it
# ...expands to //li[child::span]
//ul/li/.. # use .. to select a parent
Referenc