Twiety
Twiety

Reputation: 43

Does xpath containing preceding-sibling or following-sibling are supposed to be of same tag?

suppose my HTML structure is :

 <div class='parent'>

      <a class="child"> PI </a> 

      <div class="child>CP </div>  

      <a class="child>A </a>

      <a class="child>B </a>

  </div>

Parent div has 4 child and i need to click on the child div.Is this following
xpath valid? Or the preceding siblings means they have to be of same 'a' tag.

//a[contains(text(),'A')]/preceding-sibling::div[contains(text(),'CP')]

Upvotes: 0

Views: 2684

Answers (1)

Keith Hall
Keith Hall

Reputation: 16095

Your XPath is valid. The preceding-sibling axis selector:

Indicates all the nodes that have the same parent as the context node and appear before the context node in the source document. If the context node is an attribute node or namespace node, the preceding-sibling axis is empty

Same for following-sibling, except of course, it:

Indicates all the nodes that have the same parent as the context node and appear after the context node in the source document.

Sources:

Upvotes: 2

Related Questions