XPath

精选 XPath 常用指令与核心速查备忘单,涵盖高频用法、配置参数与实用技巧。

#XPath 选择器

#快速开始

可在 Firefox 或 Chromium 内核浏览器控制台中测试:

$x('/html/body')
$x('//h1')
$x('//h1')[0].innerText
$x('//a[text()="XPath"]')[0].click()

#后代选择器

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

#顺序选择器

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

#属性选择器

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']

#兄弟选择器

Xpath CSS
//h1/following-sibling::ul h1 ~ ul
//h1/following-sibling::ul[1] h1 + ul
//h1/following-sibling::[@id="id"] h1 ~ #id

#jQuery 对照

Xpath CSS
//ul/li/.. $('ul > li').parent()
//li/ancestor-or-self::section $('li').closest('section')
//a/@href $('a').attr('href')
//span/text() $('span').text()

#其它选择器

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)

#XPath 选择器

#快速开始

可在 Firefox 或 Chromium 内核浏览器控制台中测试:

$x('/html/body')
$x('//h1')
$x('//h1')[0].innerText
$x('//a[text()="XPath"]')[0].click()

#后代选择器

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

#顺序选择器

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

#属性选择器

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']

#兄弟选择器

Xpath CSS
//h1/following-sibling::ul h1 ~ ul
//h1/following-sibling::ul[1] h1 + ul
//h1/following-sibling::[@id="id"] h1 ~ #id

#jQuery 对照

Xpath CSS
//ul/li/.. $('ul > li').parent()
//li/ancestor-or-self::section $('li').closest('section')
//a/@href $('a').attr('href')
//span/text() $('span').text()

#其它选择器

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)

#XPath 选择器

#快速开始

可在 Firefox 或 Chromium 内核浏览器控制台中测试:

$x('/html/body')
$x('//h1')
$x('//h1')[0].innerText
$x('//a[text()="XPath"]')[0].click()

#后代选择器

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

#顺序选择器

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

#属性选择器

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']

#兄弟选择器

Xpath CSS
//h1/following-sibling::ul h1 ~ ul
//h1/following-sibling::ul[1] h1 + ul
//h1/following-sibling::[@id="id"] h1 ~ #id

#jQuery 对照

Xpath CSS
//ul/li/.. $('ul > li').parent()
//li/ancestor-or-self::section $('li').closest('section')
//a/@href $('a').attr('href')
//span/text() $('span').text()

#其它选择器

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)

#XPath 选择器

#快速开始

可在 Firefox 或 Chromium 内核浏览器控制台中测试:

$x('/html/body')
$x('//h1')
$x('//h1')[0].innerText
$x('//a[text()="XPath"]')[0].click()

#后代选择器

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

#顺序选择器

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

#属性选择器

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']

#兄弟选择器

Xpath CSS
//h1/following-sibling::ul h1 ~ ul
//h1/following-sibling::ul[1] h1 + ul
//h1/following-sibling::[@id="id"] h1 ~ #id

#jQuery 对照

Xpath CSS
//ul/li/.. $('ul > li').parent()
//li/ancestor-or-self::section $('li').closest('section')
//a/@href $('a').attr('href')
//span/text() $('span').text()

#其它选择器

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)

#XPath 选择器

#快速开始

可在 Firefox 或 Chromium 内核浏览器控制台中测试:

$x('/html/body')
$x('//h1')
$x('//h1')[0].innerText
$x('//a[text()="XPath"]')[0].click()

#后代选择器

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

#顺序选择器

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

#属性选择器

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']

#兄弟选择器

Xpath CSS
//h1/following-sibling::ul h1 ~ ul
//h1/following-sibling::ul[1] h1 + ul
//h1/following-sibling::[@id="id"] h1 ~ #id

#jQuery 对照

Xpath CSS
//ul/li/.. $('ul > li').parent()
//li/ancestor-or-self::section $('li').closest('section')
//a/@href $('a').attr('href')
//span/text() $('span').text()

#其它选择器

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)

#XPath 选择器

#快速开始

可在 Firefox 或 Chromium 内核浏览器控制台中测试:

$x('/html/body')
$x('//h1')
$x('//h1')[0].innerText
$x('//a[text()="XPath"]')[0].click()

#后代选择器

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

#顺序选择器

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

#属性选择器

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']

#兄弟选择器

Xpath CSS
//h1/following-sibling::ul h1 ~ ul
//h1/following-sibling::ul[1] h1 + ul
//h1/following-sibling::[@id="id"] h1 ~ #id

#jQuery 对照

Xpath CSS
//ul/li/.. $('ul > li').parent()
//li/ancestor-or-self::section $('li').closest('section')
//a/@href $('a').attr('href')
//span/text() $('span').text()

#其它选择器

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)

#XPath 选择器

#快速开始

可在 Firefox 或 Chromium 内核浏览器控制台中测试:

$x('/html/body')
$x('//h1')
$x('//h1')[0].innerText
$x('//a[text()="XPath"]')[0].click()

#后代选择器

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

#顺序选择器

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

#属性选择器

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']

#兄弟选择器

Xpath CSS
//h1/following-sibling::ul h1 ~ ul
//h1/following-sibling::ul[1] h1 + ul
//h1/following-sibling::[@id="id"] h1 ~ #id

#jQuery 对照

Xpath CSS
//ul/li/.. $('ul > li').parent()
//li/ancestor-or-self::section $('li').closest('section')
//a/@href $('a').attr('href')
//span/text() $('span').text()

#其它选择器

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)

#XPath 选择器

#快速开始

可在 Firefox 或 Chromium 内核浏览器控制台中测试:

$x('/html/body')
$x('//h1')
$x('//h1')[0].innerText
$x('//a[text()="XPath"]')[0].click()

#后代选择器

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

#顺序选择器

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

#属性选择器

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']

#兄弟选择器

Xpath CSS
//h1/following-sibling::ul h1 ~ ul
//h1/following-sibling::ul[1] h1 + ul
//h1/following-sibling::[@id="id"] h1 ~ #id

#jQuery 对照

Xpath CSS
//ul/li/.. $('ul > li').parent()
//li/ancestor-or-self::section $('li').closest('section')
//a/@href $('a').attr('href')
//span/text() $('span').text()

#其它选择器

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)
// ul /child:: li
Axis Step Axis Step

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 ::.

#子节点轴 (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
//ul[li]
//ul[child::li]
# both the same
//ul[count(li) > 2]
//ul[count(child::li) > 2]

#后代或自身轴 (Descendant-or-self)

# both the same
//div//h4
//div/descendant-or-self::h4

// is short for the descendant-or-self:: axis.

# both the same
//ul//[last()]
//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)

//a | //span

Use | to join two expressions.

#更多高级示例 (More Examples)

#💡 实用示例

//*                 # all elements
count(//*)          # count all elements
(//h1)[1]/text()    # text of the first h1 heading
//li[span]          # find a <li> with an <span> inside it
# ...expands to //li[child::span]
//ul/li/..          # use .. to select a parent

#向上查找父级节点 (Find Parent)

//section[h1[@id='section-name']]

Finds a <section> that directly contains h1#section-name

//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)

./ancestor-or-self::[@class="box"]

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

#动态属性计算比较 (Attributes)

//item[@price > 2*@discount]

Finds <item> and check its attributes

#🔗 参考资源