在 html 页面中,我有以下应答器:
<button class="....." data-toggle="dropdown">
Text: Title of the button
<span class="....."/>
</button>
我想通过可见文本文本:按钮的标题检测这个按钮问题是我正在尝试使用以下 Xpath 检测它
.//*[contains(text(),"Text: Title of the button")]
但它不起作用。 我怎样才能检测到这个按钮?
仅供引用:我无法使用 class
检测到它,因为与此类匹配的节点不止一个。
最佳答案
contains(text(), '...')
仅评估当前上下文元素的第一个文本节点子节点。这对于您发布的示例元素应该没有任何问题(请参阅 demo ,但它不适用于例如以下元素,因为 button
中的第一个文本节点是位于之前的换行符跨度
:
<button class="....." data-toggle="dropdown">
<span class="....."/>
Text: Title of the button
</button>
要评估当前上下文元素的所有 文本节点子节点,请改用以下形式:
.//*[text()[contains(.,"Text: Title of the button")]]
上面的 XPath 测试单个文本节点是否包含特定文本。
关于html - Selenium:通过可见文本查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37408657/