最近在搞puppeteer的自动化,有很多地方需要用到CSS Selector,但是某些地方又是用Xpath方便,金手指放一下,备查:
Test queries in the Xpath test bed:
- Xpath test bed (whitebeam.org)
Browser console
1$x("//div")
Works in Firefox and Chrome.
# Selectors
Descendant selectors
h1 |
//h1 |
|
---|---|---|
div p |
`//div//p | |
ul > li |
`//ul/li | |
ul > li > a |
//ul/li/a |
|
div > * |
//div/* |
|
:root |
`/ | |
:root > body |
/body |
Attribute selectors
#id |
//*[@id="id"] |
|
---|---|---|
.class |
//*[@class="class"] …kinda
|
|
input[type="submit"] |
//input[@type="submit"] |
|
a#abc[for="xyz"] |
`//a[@id=“abc”][@for=“xyz”] | |
a[rel] |
//a[@rel] |
|
a[href^='/'] |
`//a[starts-with(@href, ‘/’)] | |
a[href$='pdf'] |
//a[ends-with(@href, '.pdf')] |
|
a[href*='://'] |
//a[contains(@href, '://')] |
|
a[rel~='help'] |
//a[contains(@rel, 'help')] …kinda
|
Order selectors
ul > li:first-of-type |
`//ul/li[1] | |
---|---|---|
ul > li:nth-of-type(2) |
//ul/li[2] |
|
ul > li:last-of-type |
//ul/li[last()] |
|
li#id:first-of-type |
`//li[1][@id=“id”] | |
a:first-child |
//*[1][name()="a"] |
|
a:last-child |
//*[last()][name()="a"] |
Siblings
h1 ~ ul |
`//h1/following-sibling::ul | |
---|---|---|
h1 + ul |
//h1/following-sibling::ul[1] |
|
h1 ~ #id |
//h1/following-sibling::[@id="id"] |
jQuery
$('ul > li').parent() |
`//ul/li/.. | |
---|---|---|
$('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)] | |
---|---|---|
Text match | `//button[text()=“Submit”] | |
Text match (substring) | //button[contains(text(),"Go")] |
|
Arithmetic | //product[@price > 2.50] |
|
Has children | //ul[*] |
|
Has children (specific) | //ul[li] |
|
Or logic | `//a[@name or @href] | |
Union (joins results) | `//a | //div |
Class check
1//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 ).
# Expressions
Steps and axes
// |
ul |
/ |
a[@id='link'] |
---|---|---|---|
Axis | Step | Axis | Step |
Prefixes
Prefix | Example | What |
---|---|---|
// |
//hr[@class='edge'] |
Anywhere |
./ |
./a |
Relative |
/ |
/html/body/div |
Root |
Begin your expression with any of these.
Axes
Axis | Example | What |
---|---|---|
/ |
//ul/li/a |
Child |
// |
//[@id="list"]//a |
Descendant |
Separate your steps with /
. Use two (//
) if you don’t want to select direct children.
Steps
1//div
2//div[@name='box']
3//[@id='link']
A step may have an element name (div
) and predicates
([...]
). Both are optional. They can also be these other things:
1//a/text() #=> "Go home"
2//a/@href #=> "index.html"
3//a/* #=> All a's child elements
# Predicates
Predicates
1//div[true()]
2//div[@class="head"]
3//div[@class="head"][@id="top"]
Restricts a nodeset only if some condition is true. They can be chained.
Operators
1# Comparison
2//a[@id = "xyz"]
3//a[@id != "xyz"]
4//a[@price > 25]
5# Logic (and/or)
6//div[@id="head" and position()=2]
7//div[(x and y) or not(z)]
Use comparison and logic operators to make conditionals.
Using nodes
1# Use them inside functions
2//ul[count(li) > 2]
3//ul[count(li[@class='hide']) > 0]
4# This returns `<ul>` that has a `<li>` child
5//ul[li]
You can use nodes inside predicates.
Indexing
1//a[1] # first <a>
2//a[last()] # last <a>
3//ol/li[2] # second <li>
4//ol/li[position()=2] # same as above
5//ol/li[position()>1] # :not(:first-of-type)
Use []
with a number, or last()
or position()
.
Chaining order
1a[1][@href='/']
2a[@href='/'][1]
Order is significant, these two are different.
Nesting predicates
1//section[.//h1[@id='hi']]
This returns <section>
if it has an <h1>
descendant with id='hi'
.
# Functions
Node functions
1name() # //[starts-with(name(), 'h')]
2text() # //button[text()="Submit"]
3 # //button/text()
4lang(str)
5namespace-uri()
6count() # //table[count(tr)=1]
7position() # //ol/li[position()=2]
Boolean functions
1not(expr) # button[not(starts-with(text(),"Submit"))]
String functions
1contains() # font[contains(@class,"head")]
2starts-with() # font[starts-with(@class,"head")]
3ends-with() # font[ends-with(@class,"head")]
4concat(x,y)
5substring(str, start, len)
6substring-before("01/02", "/") #=> 01
7substring-after("01/02", "/") #=> 02
8translate()
9normalize-space()
10string-length()
Type conversion
1string()
2number()
3boolean()
# Axes
Using axes
1//ul/li # ul > li
2//ul/child::li # ul > li (same)
3//ul/following-sibling::li # ul ~ li
4//ul/descendant-or-self::li # ul li
5//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 |
---|---|---|---|
Axis | Step | Axis | Step |
Child axis
1# both the same
2//ul/li/a
3//child::ul/child::li/child::a
child::
is the default axis. This makes //a/b/c
work.
1# both the same
2# this works because `child::li` is truthy, so the predicate succeeds
3//ul[li]
4//ul[child::li]
5# both the same
6//ul[count(li) > 2]
7//ul[count(child::li) > 2]
Descendant-or-self axis
1# both the same
2//div//h4
3//div/descendant-or-self::h4
//
is short for the descendant-or-self::
axis.
1# both the same
2//ul//[last()]
3//ul/descendant-or-self::[last()]
Other axes
Axis | Abbrev | Notes |
---|---|---|
ancestor |
||
ancestor-or-self |
||
attribute |
@ |
@href is short for attribute::href |
child |
div is short for child::div |
|
descendant |
||
descendant-or-self |
// |
// is short for /descendant-or-self::node()/ |
namespace |
||
self |
. |
. is short for self::node() |
parent |
.. |
.. is short for parent::node() |
following |
||
following-sibling |
||
preceding |
||
preceding-sibling |
There are other axes you can use.
Unions
1//a | //span
Use |
to join two expressions.
# More examples
Examples
1//* # all elements
2count(//*) # count all elements
3(//h1)[1]/text() # text of the first h1 heading
4//li[span] # find a <li> with an <span> inside it
5 # ...expands to //li[child::span]
6//ul/li/.. # use .. to select a parent
Find a parent
1//section[h1[@id='section-name']]
Finds a <section>
that directly contains h1#section-name
1//section[//h1[@id='section-name']]
Finds a <section>
that contains h1#section-name
. (Same as above, but uses descendant-or-self instead of child)
Closest
1./ancestor-or-self::[@class="box"]
Works like jQuery’s $().closest('.box')
.
Attributes
1//item[@price > 2*@discount]
Finds <item>
and check its attributes
# References
- Xpath test bed (whitebeam.org)