xpath - 在 xpath 中禁用父元素时如何跳过选择子元素
问题描述
我有一个禁用的按钮,它还有一个子元素。现在我需要在按钮为时跳过disabled
按钮及其子元素,disabled
但以下选择器仅跳过按钮元素而不跳过其子元素。因此,当该按钮的父元素与子元素一起被禁用时,应该跳过我的情况。以下选择器应该适用于下面的两个按钮。
选择器:
//*[(@type='button' or @type='submit' or contains(@class,'btn') or contains(@class,'Button')) and (@data-nw-id='Add Time' or text()='Add Time' or @value='Add Time' or @title='Add Time' or .//*='Add Time') and not(@disabled)]
带有子元素的按钮:
<button disabled type="button" data-nw-id="Add Time" data-nw-node="ButtonTag" data-nw-file="Button">
<span data-nw-node="span" data-nw-file="Button">Add Time</span>
</button>
仅按钮:
<button disabled type="button" data-nw-id="Add Time" data-nw-node="ButtonTag" data-nw-file="Button">
Add Time
</button>
任何人都可以帮助我吗?提前致谢!!
解决方案
XPath 用于选择元素而不是跳过它们。关于您的长 XPath 表达式,您希望选择包含特定属性的“启用”元素。必须取消选择这些元素的子元素。
由于选择了按钮或按钮的子元素,因此您的 XPath 表达式可以正常工作。
您可以使用轴谓词来构建“更安全”的 XPath(我已经稍微清理了表达式):
//*[not(parent::*[@disabled]) and not(@disabled)][@type='button' or @type='submit' or contains(@class,'btn') or contains(@class,'Button')][@*='Add Time' or text()='Add Time']
这将仅选择具有特定属性且其父级不包含@disabled
属性的“启用”元素。
推荐阅读
- python - 找不到 pytest 夹具 (pytest-bdd)
- rxjs - RxJS 运算符类似于 share(),但不会阻止辅助观察者/订阅者完成订阅
- kubernetes-helm - 如何通过helm operator更改Kafka集群broker中的Kafka.properties文件
- google-apps-script - 将文件夹 id 字符串传递给调用 DriveApp.getFolderById(id).getDateCreated() 的自定义函数时,Apps 脚本会引发错误;
- mfc - 如何在 MFC TreeControl 中绘制附加线?
- reactjs - 如何通过单击按钮使用 Reactjs 中的状态更改每个单独 div 的背景颜色?
- f# - 如何使用 F# 在 Akka.Remote 中发送带有元组的消息?
- facebook - 如何获得确切目标 ID 的受众规模?
- python - 从网页中选择选项
- java - Linux 上的 MapReduce Hadoop - 更改归约键