python - 使用 xpath 选择子文本节点

标签 python selenium xpath

我正在尝试匹配文本 Test number 3在 selenium 中使用 xpath。

<span>
    <br> Test number 1
    <br> Test number 2
    <br> Test number 3
</span>

我可以获取 span 元素,然后从列表中获取最后一个文本节点,类似于 /span//text()[3]

但是我可以直接通过 xpath 检查测试号 3 是否存在,如下所示

'//span[contains(text()," Test number 3")]'

这不起作用,只有部分工作是 '//span[contains(text()," Test")]'

但是我可以匹配 Test number 3

最佳答案

您需要对 应用“包含”检查。 - a string value of the span node , text() 只会匹配第一个 text nodespan 内:

.//span[contains(normalize-space(.), "Test number 3")]

关于python - 使用 xpath 选择子文本节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48104633/

相关文章:

python - 如何使用 python 读取微秒精度的 mysql 日期时间字段

python - 最小化 RDP(VM) 时自动化脚本失败

python - 在 python 中使用 xpath 在所有 css 选择器中搜索整个页面中的特定单词

带有重复键或无的 Python dict

python - 如何将 mm :ss. ms 转换为 ss.ms?

python - 结合 Selenium-Wire 和 Selenium-Request

xpath - RobotFramework:查找包含文本的 Xpath

xml - XSL身份副本

python .count : Can't find correct syntax to count\n

javascript - Selenium IDE gotoif 变量包含字符串?