XPath定位手册
XPath定位手册
一、XPath核心定位方法
1.基础路径定位
(1)绝对路径定位
从根节点开始的完整路径,以/开头:
/html/body/div/form/input[@name='password']
(2)相对路径定位
以//开头,从当前节点开始搜索:
//input[@name='password']
2.位置索引定位
通过在 [] 中填写规则进行定位:
//ol/li[3] (第三个li元素)
//table/tr[last()] (最后一行)
//tr[position()<4] (前三行)
3. 属性定位法
通过元素属性值精准匹配,@+属性名=xx值:
//a[@href='https://example.com']
//img[@alt='logo']
//div[@class='header active']
4. 逻辑运算定位
通过逻辑运算匹配出符合条件的元素:
//div[@class='error' or @id='alert']
//input[@type='checkbox' and not(@disabled)]
5. 文本内容定位
(1)精确匹配
通过 text()= 进行精准匹配:
//p[text()='欢迎光临']
(2)模糊匹配
通过 contains() 或 starts-with() 或 ends-with() 进行模糊匹配:
//a[contains(@href, 'logout')]
//h1[starts-with(@class, 'heading-')]
6. 层级关系定位
(1)父子关系
//div[@id='parent']/child::span
(2)祖先-后代关系
//form//input[@type='submit'] (form下的任意层级input)
(3)兄弟关系
//h2[@class='title']/following-sibling::p
//ul[@id='menu']/li[1]/preceding-sibling::li
二、XPath定位黄金原则
1. 稳定性优先原则
- 避免绝对路径:优先使用相对路径
- 慎用位置索引:优先通过属性/文本定位
- 规避动态属性:如style、onclick等易变属性
2. 性能优化原则
- 缩短路径长度:避免全文档扫描
//* - 优先ID/Class:浏览器对标准属性优化更好
- 减少函数调用:
contains()比正则更高效
3. 容错设计原则
- 多方案备份:准备2-3种定位方式
# Python示例:备选定位策略
try:
element = driver.find_element(By.XPATH, "//a[@id='primary']")
except:
element = driver.find_element(By.XPATH, "//nav//a[text()='首页']")
- 模糊匹配:对动态ID采用部分匹配
//div[starts-with(@id, 'temp-')]
4. 跨浏览器兼容原则
- 避免XPath 2.0+特性:如if-then-else
- 慎用轴定位:如following-axis在某些浏览器支持差
- 优先CSS选择器:对简单定位可混合使用
附录:函数汇总
1. 字符串处理函数
| 函数 | 作用 | 示例 |
|---|---|---|
| contains() | 检查字符串是否包含子串 | //div[contains(@id, 'temp-')] |
| starts-with() | 检查字符串是否以子串开头 | //input[starts-with(@name, 'user-')] |
| ends-with() | 检查字符串是否以子串结尾(XPath 2.0+) | //a[ends-with(@href, '.pdf')] |
| normalize-space() | 去除首尾空格并合并中间空格 | //p[normalize-space(text())='Hello'] |
| substring() | 截取字符串 | //div[@id=substring('abc-123', 5)] |
| translate() | 字符替换 | //input[@value=translate('HELLO', 'HEL', 'hel')] |
2. 逻辑运算函数
| 函数 | 作用 | 示例 | 含义 |
|---|---|---|---|
| not() | 逻辑非 | //input[not(@disabled)] | 选择所有没有 `disabled` 属性的 `input` 元素 |
| boolean() | 转换为布尔值 | //div[boolean(@data-valid)] | 选择所有具有 `data-valid` 属性的 `div` 元素 |
| and | 逻辑与 | input[@type='text' and @value='email'] | 选择所有 `type` 属性等于 `text` 且 `value` 属性等于 `email` 的 `input` 元素 |
| or | 逻辑或 | input[@type='text' or @type='email'] | 选择所有 `type` 属性等于 `text` 或 `type` 属性等于 `email` 的 `input` 元素 |
3. 数值计算函数
| 函数 | 作用 | 示例 |
|---|---|---|
| count() | 统计节点数 | //table[count(tr) > 5] |
| sum() | 求和(XPath 2.0+) | sum(//price) |
4. 轴函数
| 函数 | 方向 | 作用 | 示例 |
|---|---|---|---|
| child:: | 向下 | 所有子节点(默认轴,可省略) | child::p 或 p |
| parent:: | 向上 | 直接父节点 | //h2/parent::div |
| ancestor:: | 向上 | 所有祖先节点(父节点、祖父节点等) | //td/ancestor::table |
| descendant:: | 向下 | 所有后代节点(子节点、孙节点等) | //td/descendant::span |
| following:: | 文档顺序向后 | 当前节点之后的所有节点(不包括后代) | //h2/following::p[1] |
| following-sibling:: | 向后 | 当前节点之后的同级兄弟节点(同一父节点下) | //h2/following-sibling::li |
| preceding:: | 文档顺序向前 | 当前节点之前的所有节点(不包括祖先) | //li[last()]/preceding::li |
| preceding-sibling:: | 向前 | 当前节点之前的同级兄弟节点(同一父节点下) | //li[last()]/preceding-sibling::a |

浙公网安备 33010602011771号