在 XPath 中,父级和祖先有什么区别?

Gok*_*kul 4 html xml xpath webdriver

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

  1. 父母
  2. 祖先

ancestor[1]等于parent? IE,

//*[text()='target_text']//ancestor::div[1]
Run Code Online (Sandbox Code Playgroud)

等于

//*[text()='target_text']//parent::div
Run Code Online (Sandbox Code Playgroud)

kjh*_*hes 8

parent::ancestor::axis之间的区别通过它们的名称来表达:父级是直接的直接祖先

所以,对于这个 XML,例如,

<a>
  <b>
    <c>
      <d/>
    </c>
  </b>
</a>
Run Code Online (Sandbox Code Playgroud)
  • /a/b/c/d/parent::* 选择 c
  • /a/b/c/d/ancestor::*选择c, b, 和a

所以, yes/a/b/c/d/ancestor::*[1]将与/a/b/c/d/parent::*.

  • 解释得很好。 (2认同)