<分区>
我正在使用以下 Xpath 为 Selenium 构建汇总:
//a[contains(@title, "(' + args.Code + ')")]
//a[@title=\'' + args.Code + '\']
但是我需要这个参数不区分大小写,这样运行我的脚本的人就不会因为添加大写或小写字母而搞砸了。
值得一提的是,键/属性代码会根据用户在汇总开始时写入的内容而改变。
我一直在努力寻找解决方案,但还没有找到。有人知道怎么做吗?
<分区>
我正在使用以下 Xpath 为 Selenium 构建汇总:
//a[contains(@title, "(' + args.Code + ')")]
//a[@title=\'' + args.Code + '\']
但是我需要这个参数不区分大小写,这样运行我的脚本的人就不会因为添加大写或小写字母而搞砸了。
值得一提的是,键/属性代码会根据用户在汇总开始时写入的内容而改变。
我一直在努力寻找解决方案,但还没有找到。有人知道怎么做吗?
最佳答案
来自 this very similar question , one of the answers , 通过 kjhughes , 提供这些解决方案:
XPath 2.0 Solutions
- Use lower-case():
/html/body//text()[contains(lower-case(.),'test')]
- Use matches() regex matching with its case-insensitive flag:
/html/body//text()[matches(.,'test', 'i')]
因此,在您的示例中采用第一种方法(即 lower-case()):
//a[contains(lower-case(@title), "(' + lower-case(args.Code) + ')")]
//a[lower-case(@title)=\'' + lower-case(args.Code) + '\']
来自 this answer :
upper-case()
andlower-case()
are XPath 2.0 functions. Chances are your platform supports XPath 1.0 only1
尝试第一个解决方案(如 Tomalak 在 the answer 中所建议的那样):
//a[contains(translate(@title, \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\'), "(translate(' + args.Code + ', \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\')")]
//a[translate(@title, \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\')=translate(\'' + args.Code + '\', \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\') ]
Unfortunately, this requires knowledge of the alphabet the text uses. For plain English, the above probably works, but if you expect accented characters, make sure you add them to the list.1
关于javascript - 如何检查属性的文本是否包含部分参数标题(不区分大小写)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44762758/
相关文章:
javascript - Jquery 验证带有索引的复选框
javascript - 我可以使用 Javascript 为同一个 CSS 属性设置多个值吗?
jquery - XPath 在 JQuery 选择器中不起作用(JQuery 版本 1.7.1)
java - Selenium WebDriver 中隐式 Waits() 中的错误
javascript - 消费者和 vendor 的 LTI 合规示例
javascript - 如何使用 jQuery 并行处理动态添加的脚本标签
java - xpath 在 selenium 中无法识别,但在 XPather 中可以识别