python - XPath:通过*纯*文本查找 HTML 元素

标签 python xpath selenium

请注意:可以找到此问题的更精确版本,并提供适当的答案 here .

我想使用 Selenium Python 绑定(bind)在网页上查找具有给定文本的元素。例如,假设我有以下 HTML:

<html>
    <head>...</head>
    <body>
        <someElement>This can be found</someElement>
        <someOtherElement>This can <em>not</em> be found</someOtherElement>
    </body>
</html>

我需要按文本搜索并能够找到 <someElement>使用以下 XPath:

//*[contains(text(), 'This can be found')]

我正在寻找可以让我找到 <someOtherElement> 的类似 XPath使用 文本 "This can not be found" .以下不起作用:

//*[contains(text(), 'This can not be found')]

我知道这是因为嵌套的 em “中断”“无法找到”文本流的元素。是否有可能通过 XPath 以某种方式忽略与上述类似的嵌套?

最佳答案

您可以使用 //*[contains(., 'This can not be found')] .

上下文节点.在与“This can not be found”比较之前将被转换为其字符串表示形式。

不过要小心,因为您正在使用 //* , 因此它将匹配包含此字符串的 ALL englobing 元素。

在您的示例中,它将匹配:

  • <someOtherElement>
  • <body>
  • <html> !

您可以通过定位文档中的特定元素标签或特定部分来限制这一点(<table><div> 具有已知的 id 或类)


在关于如何找到与文本条件匹配的最多嵌套元素的评论中编辑 OP 的问题:

The accepted answer here建议 //*[count(ancestor::*) = max(//*/count(ancestor::*))]选择最嵌套的元素。我认为它只是 XPath 2.0。

结合您的子字符串条件,我能够 test it here附上这份文件

<html>
<head>...</head>
<body>
    <someElement>This can be found</someElement>
    <nested>
        <someOtherElement>This can <em>not</em> be found most nested</someOtherElement>
    </nested>
    <someOtherElement>This can <em>not</em> be found</someOtherElement>
</body>
</html>

和这个 XPath 2.0 表达式

//*[contains(., 'This can not be found')]
   [count(ancestor::*) = max(//*/count(./*[contains(., 'This can not be found')]/ancestor::*))]

并且匹配包含“This cannot be found most nested”的元素。

可能有更优雅的方法来做到这一点。

关于python - XPath:通过*纯*文本查找 HTML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18655765/

相关文章:

python - 如何使用 Tornado 在前端显示错误

java - XPath:生成从根节点到指定节点的相对表达式?

java - 从 XPath 表达式填充 XML 模板文件?

java - 如何在 selenium webdriver 中使用 css 检查按钮是否被禁用

javascript - Selenium 拆分日期字符串工作示例?

selenium - 如何从在新窗口中打开的WebDriver实例获取页面的URL

python - 按照教程进行:无法运行Django的Docker容器

python - Scrapy - 解析页面以提取项目 - 然后跟踪并存储项目 url 内容

google-chrome - 在 Firefox 和 Chrome 浏览器中保留缓存 - Selenium WebDriver

python - 权限错误 : [WinError 32] The process cannot access the file because it is being used by another process