Search code examples
xpath

How to get a parent element by two child elements using XPATH?


I have a list of the following structure:

<ul>
   <li>
     <a href="/link1">
         <i class="icon-soccer"></i>
         <label><span>Barcelona<span></label>
     </a>
   </li>
   <li>
     <a href="/link2">
         <i class="icon-baseball"></i>
         <label><span>Barcelona<span></label>
     </a>
   </li>
   <li>
     <a href="/link3">
         <i class="icon-soccer"></i>
         <label><span>Milan FC<span></label>
     </a>
   </li>
</ul>

How do I get the link element in such a way that the "i" element contains class="icon-soccer" and the "span" Barcelona element?

I tried something like this code

//span[text() = "Barcelona"]/preceding::i[@class="icon-soccer"]/parent::a

But it doesn't work as it's supposed to


Solution

  • This xpath select the expected anchor

    //a[i[@class="icon-soccer"] and .//span[.="Barcelona"]]