如何检查属性的文本是否包含部分参数标题(不区分大小写)?

How can I check if the text of an attribute Contains a partial argument title (case insensitive)?

我正在使用以下 Xpath 为 Selenium 构建 roll-up:

//a[contains(@title, "(' + args.Code + ')")]

//a[@title=\'' + args.Code + '\']

但是我需要这个参数不区分大小写,这样运行我的脚本的人就不会因为添加大写或小写字母而搞砸了。

值得一提的是,key/property 代码 会根据用户在 roll-up 开头写的内容而改变。

我一直在努力寻找解决方案,但还没有找到。有人知道怎么做吗?

来自 this very similar question, one of the answers, by kjhughes,提供这些解决方案:

XPath 2.0 Solutions

  1. Use lower-case():
    /html/body//text()[contains(lower-case(.),'test')]
  2. 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() and lower-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


1