【问题标题】:XPath get element with text in child elementXPath 获取子元素中带有文本的元素
【发布时间】:2019-03-07 15:45:16
【问题描述】:

我想将<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'] 只给我最高级别的结果。

如何匹配各级文字?

【问题讨论】:

  • XML 和 HTML 中的属性不以 @ 开头——您的示例将标记语法与 XPath 语法混淆了。希望这是一个问题错字,而不是您的实际数据。

标签: html xml xpath


【解决方案1】:

尝试以下方法:

//div/button[descendant::text()="Close"]

【讨论】:

    【解决方案2】:

    这个 XPath,

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

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

    【讨论】:

      猜你喜欢
      • 2020-03-24
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多