【问题标题】:In XPath what is the difference between parent and ancestor?在 XPath 中,父级和祖先级有什么区别?
【发布时间】:2019-02-26 15:17:30
【问题描述】:

我在 XPath 中看到 2 个不同的轴

  1. 父母
  2. 祖先

ancestor[1] 是否等于parent?即,

//*[text()='target_text']//ancestor::div[1]

等于

//*[text()='target_text']//parent::div

【问题讨论】:

    标签: html xml xpath webdriver


    【解决方案1】:

    parent::ancestor:: 轴之间的区别是通过它们的名称来传达的: parent 是直接直接的祖先

    因此,以这个 XML 为例,

    <a>
      <b>
        <c>
          <d/>
        </c>
      </b>
    </a>
    
    • /a/b/c/d/parent::* 选择c
    • /a/b/c/d/ancestor::* 选择 cba

    所以,是的,/a/b/c/d/ancestor::*[1]/a/b/c/d/parent::* 相同。

    【讨论】:

    • 解释得很好。
    • 很好的解释,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多