博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xpath如何通过class来定位元素
阅读量:3557 次
发布时间:2019-05-20

本文共 1120 字,大约阅读时间需要 3 分钟。

xpath中没有提供对class的原生查找方法。但是我在stackoverflow上看到了一个很有才的回答。现翻译如下

原帖链接(不管你能不能看到,总之原帖都是要贴的,要看到请自带天梯):

翻译如下

This selector should work but will be more efficient if you replace it with your suited markup:

这个表达式应该是可行的。不过如果你把class换成更好识别的标识执行效率会更高

//*[contains(@class, 'Test')]
But since this will also match cases like class="Testvalue" or class="newTest", @Tomalak's version provided in the comments is better:

但是这个表达式会把类似 class="Testvalue" 或者 class="newTest"也匹配出来。 @Tomalas 在评论里面的版本更好

//*[contains(concat(' ', @class, ' '), ' Test ')]

If you wished to be really certain that it will match correctly, you could also use the normalize-space function to clean up stray whitespace characters around the class name (as mentioned by @Terry):

如果你希望匹配的结果尽量精确,你还可以使用 normalize-space 功能来清除class名称周围的空格

//*[contains(concat(' ', normalize-space(@class), ' '), ' Test ')]
Note that in all these versions, the * should best be replaced by whatever element name you actually wish to match, unless you wish to search each and every element in the document for the given condition.

请注意在所有这些版本里,除非你想要在所有元素里搜索带有这些条件的元素,否则你最好把*号替换成你想要匹配的具体的元素名(标签名)。

转载地址:http://wjdrj.baihongyu.com/

你可能感兴趣的文章
如何设计一个秒杀系统 -- 动静分离方案
查看>>
JWT 快速了解
查看>>
实习日志一
查看>>
Springboot读取自定义配置文件的几种方法
查看>>
ZigbeeCC2530 --(裸机和协议栈)串口时钟配置
查看>>
ZigBee开发环境搭建 ----IAR for 8051与SmartRFProgram等软件安装使用
查看>>
Python ---太空射击游戏
查看>>
C/C++之struct的小知识
查看>>
温湿度传感器(AM2312)
查看>>
ubuntu安装
查看>>
TCP_QT
查看>>
IDE报错
查看>>
牛客_整数1出现的次数
查看>>
牛客_旋转数组
查看>>
类中类
查看>>
牛客_二维数组
查看>>
牛客_调整数组顺序
查看>>
牛客_顺时针打印矩阵
查看>>
牛客_数组中出现超过一半的数字
查看>>
图神经网络task_05
查看>>