KunLun
KunLun

Reputation: 3227

XPath shortest path between element and contains element

Let's have this xpath:

<div>
   <!-- ... -->
   <div> <!--I want this element-->
      <!-- ... -->
      <sometag>
         <a></a>
      </sometag>
      <!-- ... -->
   </div>
   <!-- ... -->
</div>

I have this xpath //div[.//a], which selects both divs.

How to write the xpath which selects the nearest div of his child(a). Without link it by that sometag.

Upvotes: 1

Views: 70

Answers (1)

har07
har07

Reputation: 89305

You can try to locate the a elements first and then select nearest ancestor div from there:

//a/ancestor::div[1]

xpathfiddle demo

Upvotes: 2

Related Questions