首页 > 解决方案 > 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']只给我最高级别的结果。

如何匹配所有级别的文本?

标签: htmlxmlxpath

解决方案


这个 XPath,

//button[normalize-space() = 'Close']

将按要求选择button空间标准化字符串值为 的所有元素'Close',而不考虑任何其他包装元素。


推荐阅读