xpath中ends-with无法定位问题的解决办法

在使用xpath的模糊匹配以什么结尾时,遇到如下问题:    //input[ends-with(@id,'w')] 定位不到input标签中的id以w结尾的元素,报错如下

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[ends-with(@id,'w')]' is not a valid XPath expression.
(Session info: chrome=94.0.4606.81)

查询相关资料后发现问题发生的原因是:ends-with方法是xpath 2.0的语法,而浏览器只支持xpth 1.0,所以出现了以上问题。

 

解决办法:

根据大神的说法可以用如下等价于ends-with的的方法代替:

//input[substring(@id, string-length(@id) - string-length('w') +1) = 'w']

 

参考内容如下,其链接是:https://stackoverflow.com/questions/36053559/how-to-locate-dynamic-element-using-xpathends-with-function-not-working

 

 

posted @ 2021-10-14 10:05  启秀  阅读(377)  评论(0编辑  收藏  举报