Need the children of an XPATH parent that has another specific children without direct correlation

Viewed 35

I need to get a specific set of elements with XPATH. I need to get all spans that have the class objective as long as the ancestor div they're in has a children with the span class "objective-is-useful".

Unfortunately the spans are not direct children and they're contained among lots of containers in-between.

I attached an example like so:

<div class="superparent">
   <div class="parent">
     <div class="container">
       <span class="objective">Data 1</span>
     </div>
     <div class="key">
       <span class="objective-is-useful">Approved</span>
     </div>
   </div>
   <div class="parent">
     <div class="container">
       <span class="objective">Data 2</span>
     </div>
     <div class="key">
     </div>
   </div>
   <div class="parent">
     <div class="container">
       <span class="objective">Data 3</span>
     </div>
     <div class="key">
       <span class="objective-is-useful">Approved</span>
     </div>
   </div>
</div>

Is there any way to make an XPATH that'd return Data 1 and Data 3 but not Data 2?

1 Answers

This should work:

//div[.//span[@class="objective-is-useful"] and(@class="parent")]//span[@class="objective"]
Related