绝对定位:

  1. /html/body/div/input[@value='查询']

如果页面动态特性很多, 很容易找不到

相对定位:

  1. //input[@value='查询']

开头的 // 表示在所有层级找.

根据id搜索:

  1. //*[@id='form']/input[1]

//*表示所有元素, 方括号的内容起到过滤作用

根据属性搜索:
id/value也是一种属性

  1. //*[@id='form']

xpath函数

  1. //img[contains(@src, 'img.baidu.com')]
  2. //a[contains(text(), '百度')]

找到父亲元素

  1. ./..
  2. ..
  3. parent::*
  4. ./parent::*

祖先节点

  1. ./ancestor::*

所有子孙节点

  1. descendant

之后的节点
following

之后的同级节点
follo

之前的同级节点
preceding

之前的同级节点
preceding-sibling

谓语
方括号里的是谓语

  1. /book[1]
  2. /book[last()]
  3. /book[last()-1]
  4. /book[position()<3]
  5. /book[@alt] 指定了altbook
  6. /book[@alt='foo'] 指定了alt=foobook
  7. // 这个price怎么理解?
  8. /bookstore/book[price>35.00]

并集

  1. //book/title | //book/price

and or 可以组合条件

函数

  1. text()
  2. 取节点的直接text, 不包含子孙的text
  3. string()
  4. 该节点及其子孙的text
  5. upper-case
  6. lower-case
  7. contains
  8. starts-with
  9. ends-with
  10. matches("str", "pattern")
  11. not(...) 对内容取反
  12. position() 当前元素在父节点下的顺序
  13. last() 返回最后一个元素的index(基于1)