使用 text() 检索包含的 xpath 文本 [英] Retrieve an xpath text contains using text()

查看:33
本文介绍了使用 text() 检索包含的 xpath 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了这个几个小时,但我就是想不通.使用 XPath 查找文本值很棘手,而且这个问题有太多可移动的部分.

I've been hacking away at this one for hours and I just can't figure it out. Using XPath to find text values is tricky and this problem has too many moving parts.

我有一个带有大表的网页,该表中的一个部分包含分配给特定单位的用户(受让人)列表.几乎总是有多个用户分配给一个单位,我需要确保将特定用户分配给桌子上的任何单位.我几乎所有的选择器都使用了 XPath,而且我已经完成了这个选择器的一半.我似乎无法弄清楚如何在这种情况下将 containstext() 一起使用.

I have a webpage with a large table and a section in this table contains a list of users (assignees) that are assigned to a particular unit. There is nearly always multiple users assigned to a unit and I need to make sure a particular user is assigned to any of the units on the table. I've used XPath for nearly all of my selectors and I'm half way there on this one. I just can't seem to figure out how to use contains with text() in this context.

这是我目前所拥有的:

//td[@id='unit']/span [text()='asdfasdfasdfasdfasdf (Primary); asdfasdfasdfasdfasdf, asdfasdfasdfasdf; 456, 3456'; testuser]

上面的 XPath 查询捕获了我正在查看的特定部分中的所有文本,这很棒.但是,我只需要知道 testuser 是否在该部分.

The XPath Query above captures all text in the particular section I am looking at, which is great. However, I only need to know if testuser is in that section.

推荐答案

text() 为您提供一组文本节点.我倾向于在//span//text() 或其他内容的上下文中更多地使用它.

text() gets you a set of text nodes. I tend to use it more in a context of //span//text() or something.

如果您试图检查元素内的文本是否包含某些内容,您应该在元素上使用 contains 而不是 text() 的结果,如下所示:

If you are trying to check if the text inside an element contains something you should use contains on the element rather than the result of text() like this:

span[contains(., 'testuser')]

XPath 非常适合上下文.如果您确切地知道节点应该具有什么文本,您可以执行以下操作:

XPath is pretty good with context. If you know exactly what text a node should have you can do:

span[.='full text in this span']

但是如果你想做正则表达式之类的事情(例如使用 exslt),你需要使用 string() 函数:

But if you want to do something like regular expressions (using exslt for example) you'll need to use the string() function:

span[regexp:test(string(.), 'testuser')]

这篇关于使用 text() 检索包含的 xpath 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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