在 selenium IDE 中使用大写和小写 xpath 函数 [英] Using upper-case and lower-case xpath functions in selenium IDE

查看:33
本文介绍了在 selenium IDE 中使用大写和小写 xpath 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 xpath 函数 lower-caseupper-case 获取 xpath 查询,但它们似乎在 selenium 中不起作用(我在其中测试了在我应用它之前的 xpath).

I am trying to get a xpath query using the xpath function lower-case or upper-case, but they seem to not work in selenium (where I test my xpath before I apply it).

不起作用的例子:

//*[.=upper-case('some text')]

我可以在复杂的路径中定位我需要的节点,甚至使用聚合函数,只要我不使用大写和小写.

I have no problem locating the nodes I need in complex path and even using aggregated functions, as long as I don't use the upper and lower case.

有没有人遇到过这种情况?有意义吗?

Has anyone encountered this before? Does it make sense?

谢谢.

推荐答案

upper-case()lower-case() 是 XPath 2.0 函数.您的平台可能仅支持 XPath 1.0.

upper-case() and lower-case() are XPath 2.0 functions. Chances are your platform supports XPath 1.0 only.

试试:

translate('some text','abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')

这是 XPath 1.0 的方法.不幸的是,这需要了解文本使用的字母表.对于简单的英语,上述方法可能有效,但如果您需要重音字符,请确保将它们添加到列表中.

which is the XPath 1.0 way to do it. 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.

在大多数环境中,您在某种宿主语言之外使用 XPath,并且可以使用宿主语言的功能来解决此 XPath 1.0 限制,方法是从外部向 translate().

In most environments you are using XPath out of a host language of some sort, and can use the host language's capabilities to work around this XPath 1.0 limitation by externally providing upper- and lower-case variants of the search string to translate().

以Python为例:

search = 'Some Text'
lc = search.lower()
uc = search.upper()

xpath = f"//p[contains(translate(., '{lc}', '{uc}'), '{uc}')]"

这将产生以下 XPath 表达式:

This would produce the following XPath expression:

//p[contains(translate(., 'some text', 'SOME TEXT'), 'SOME TEXT')]

不区分大小写搜索并适用于任意搜索文本.

which searches case-insensitively and works for arbitrary search text.

这篇关于在 selenium IDE 中使用大写和小写 xpath 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆