在 selenium IDE 中使用大写和小写 xpath 函数

发布于 2024-08-09 03:25:51 字数 292 浏览 4 评论 0原文

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

不起作用的示例:

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

只要我不使用大小写,我就可以在复杂路径中定位所需的节点,甚至使用聚合函数都没有问题。

以前有人遇到过这种情况吗?有道理吗?

谢谢。

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).

Example that does NOT work:

//*[.=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?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

近箐 2024-08-16 03:25:51

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

尝试一下:

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

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


在大多数环境中,您在某种宿主语言之外使用 XPath,并且可以使用宿主语言的功能通过外部提供搜索字符串的大小写变体来解决此 XPath 1.0 限制 translate( )

如 Python 示例所示:

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

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

这将生成以下 XPath 表达式:

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

其搜索不区分大小写,适用于任意搜索文本。

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

Try:

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

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.


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().

Shown on the example of Python:

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

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

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.

寒尘 2024-08-16 03:25:51

如果您需要在 xslt 中的多个位置使用大写字母,您可以为小写字母和大写字母定义变量,然后在翻译函数中随处使用它们。它应该使您的 xslt 更加干净。

XSL/XPATH 的示例:无大写-MSXML 4.0 中的 case 函数?

If you are going to need upper case in multiple places in your xslt, you can define variables for the lower case and upper case and then use them in your translate function everywhere. It should make your xslt much cleaner.

Example at XSL/XPATH : No upper-case function in MSXML 4.0 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文