html - XPath 获取子元素中带有文本的元素
问题描述
我想将一个<button>
元素与某个文本匹配,该文本有时会在按钮内的另一个元素中关闭,例如:
<div @class="buttonset">
<button>Close</button>
</div>
<div @class="buttonset">
<button>
<span>Close</span>
</button>
</div>
xpath 查询//div[@class='modal-buttonset']/button[text()='Cancel']
只给我最高级别的结果。
如何匹配所有级别的文本?
解决方案
这个 XPath,
//button[normalize-space() = 'Close']
将按要求选择button
空间标准化字符串值为 的所有元素'Close'
,而不考虑任何其他包装元素。
推荐阅读
- three.js - 三.js。为什么 Spotlight 不允许重置近/远?
- python - 如何使用不同的 URL 重试失败的抓取?
- javascript - 如何使用 DataTables 导出用户定义的数据?
- python - Python 命令行选项解析
- jvm - Java 应用程序的 CRIU 用法
- image - 无法提取图像“xx.azurecr.io/xx:latest”:rpc 错误:代码 = 未知 desc = 来自守护进程的错误响应:未经授权:需要身份验证
- javascript - 在方法 GET axios 中发送正文
- wix - 如何在升级时保留配置文件但在卸载时将其删除?
- sql - 在 R 中更快地读取 SQL 表
- android - 使用 shell 命令获取应用程序使用的 Memory-RAM 信息