微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

selenium使用Xpath定位方法

一、xpath的定位方法

1.通过绝对路径方式定位  (复制完整xpath就是绝对路径

/html/body/div[1]/div[2]/div[5]/div[1]/div/form/span[1]/input

2.通过相对路径方式定位(两个斜杠)

//input

3、通过元素索引定位

//input[2]

4、通过属性定位

使用xpath属性定位(结合第2、第3中方法可以使用)

//input[@id='kw']
//input[@type='name' and @name='kw']

5、通过文本定位

//a[text()='网盘']

//span[text()='按图片搜索']

6、通过部分属性值匹配

//input[starts-with(@id,'k')]
//input[ends-with(@id,'w')] chrome版本问题不行
//input[contains(@id,'w')]

三、关于xpath函数使用举例说明

1、contains():

//input[contains(@class,'ipt')] ,表示选择class中包含有’ipt’的input节点

2、text()

//span[text()='按图片搜索'] ,用text()函数来匹配节点

3、last()

(//input[@type='hidden'])[last()],取xpath最后一个  //input[@type='hidden']  元素

(//input[@type='hidden'])[last()-1] ,取xpath最后第二个  //input[@type='hidden']  元素

4、starts-with()

//input[starts-with(@id,'k')]  表示选择以’k’开头的id属性的input节点

5、not()
not()函数,表示否定

//input[@name=‘identity’ and not(contains(@class,‘a’))] ,表示匹配出name为identity并且class的值中不包含a的input节点。

//input[not(@id) and @name='rsv_spt']  表示匹配出name为rsv_spt并且不包含id的input节点。

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐