首页 > 解决方案 > 在 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 表达式可以正常工作。

您可以使用轴谓词来构建“更安全”的 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属性的“启用”元素。


推荐阅读