问题描述
在html页面中,我有以下用途:
In the html page i have the following balise:
<button class="....." data-toggle="dropdown">
Text: Title of the button
<span class="....."/>
</button>
我想检测到此按钮,可见文本文本:按钮的标题是一个问题,我正在尝试使用以下Xpath进行检测
And I want to detect this buton By the visible text Text: Title of the button the problem is that i'm trying to detect it using the following Xpath
.//*[contains(text(),"Text: Title of the button")]
但是它不起作用.我如何检测到此按钮?
But it does'nt work.How i can detect this button?
仅供参考::我无法使用class
进行检测,因为该类中有多个匹配节点.
FYI: I can't detect it using class
because there more than one matching node with this class.
推荐答案
contains(text(), '...')
仅计算当前上下文元素中的第一个文本节点子对象.对于您发布的示例元素来说,这应该不成问题(请参见演示,但不会如此)例如,可以使用以下元素,因为button
中的第一个文本节点是位于span
之前的换行符:
contains(text(), '...')
only evaluates first text node child from current context element. This shouldn't be any problem for sample element you posted (see demo, but it wouldn't work with the following element for example, because the first text node inside button
is the newline located before span
:
<button class="....." data-toggle="dropdown">
<span class="....."/>
Text: Title of the button
</button>
要评估当前上下文元素的 all 个文本节点子代,请改用以下形式:
To evaluate all text node children of current context element, use the following form instead :
.//*[text()[contains(.,"Text: Title of the button")]]
上面的XPath测试单个文本节点是否包含某些文本.
The above XPath tests individual text node if it contains certain text.
这篇关于硒:通过可见文本查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!