0
votes

I am trying to write Xpath to uniquely identify one element The document looks something like this

<AAA>
   <div>   </div>
   <div>   </div>
   <div>
      <div id='d'>YES</div>
   </div>
   <div>   </div>  ---> Want to select based on the mentioned sibling condition
   <div>   </div>

</AAA>

I want to select the div element who is following sibling of the div element, containing the child div element[containing YES]. I tried doing this which failed:

//following-sibling::div[@id='d']/div[contains(. , 'YES')]

Please help.

2

2 Answers

2
votes

The XPATH to select following-sibling div elements of any div containing YES will be:

//div[contains(., 'YES')]/following-sibling::div

In your current XML, the third div and it's child div both contain YES as their value. The child doesn't have a following-sibling here. So, only the last two div elements(which are following-sibling to the third div in your XML) will be selected.

EDIT

//div[div[contains(., 'YES')]]/following-sibling::div[1]

This would select the first following-sibling div element of any div with a child div(with value YES).

2
votes

You can try this way :

//div[div[@id='d' and contains(.,'YES')]]/following-sibling::div[1]