万能的xpath定位技巧

控件定位符的规范
使用优先级ID>name>classname>xpath,a节点的除了id、name、className,还可以优先使用linkText、partialLinkText

Xpath使用技巧

xpath长度尽量精简且能唯一标识控件,文本可以采用模糊匹配

合理使用通配符*、 // 、及/

1、xpath:://*[ @id=’xxx’]/a,*代表查找全局,通用性强

2、xpath::/span/a[text()=’xxx’],

xpath:://a[text()=’xxx’]

3、xpath:://*[contains(text(),'同意协议并注册')]

xpath:://h3[text()=’苏宁物流新闻测试’]

//*[text()="取消"]/preceding-sibling::span

XPath轴(XPath Axes)可定义某个相对于当前节点的节点集:
1、child 选取当前节点的所有子元素
//*[@class="rightTabContent"]/child::div[1] 当前节点的第一个子元素
//*[@class="personalCenterContent"]/child::div
2、parent 选取当前节点的父节点
//*[text()='我的快递']/parent::div
3、descendant 选取当前节点的所有后代元素(子、孙等)
//*[@class="leftTabContent"]/descendant::div
4、ancestor 选取当前节点的所有先辈(父、祖父等)
//*[text()='我的快递']/ancestor::div
5、descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身
//*[@class="leftTabContent"]/descendant-or-self::div
6、ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身
//*[text()='我的快递']/ancestor-or-self::div
7、preceding-sibling 选取当前节点之前的所有同级节点
//*[text()="取消"]/preceding-sibling::span
8、following-sibling 选取当前节点之后的所有同级节点
//*[@class="nodeTab"]/following-sibling::div
//*[text()='我的寄件']
9、preceding 选取文档中当前节点的开始标签之前的所有节点
//*[text()='我的寄件']/preceding::div
10、following 选取文档中当前节点的结束标签之后的所有节点
//*[text()='我的寄件']/following::div
11、self 选取当前节点
//*[text()='我的寄件']/self::div
12、attribute 选取当前节点的所有属性

//a[text()="加入收藏"]/namespace::*

13、namespace 选取当前节点的所有命名空间节点

原文地址:https://www.cnblogs.com/51testing/p/13805191.html