Search code examples
c#html-agility-pack

How to get a specific a tag?


I'm using HtmlAgilityPack and I'm trying to get a specific a tag which contains a particular text inside. Suppose that I have this html structure:

<ul>
    <li class="current">
        <a href="/matches/2018/05/20/italy/serie-a/ss-lazio-roma/fc-internazionale-milano/2539153/">Summary</a>
    </li>
    <li class="">
        <a href="/matches/2018/05/20/italy/serie-a/ss-lazio-roma/fc-internazionale-milano/2539153/head2head/">H2H Comparison</a>
    </li>
    <li class="">
        <a href="/matches/2018/05/20/italy/serie-a/ss-lazio-roma/fc-internazionale-milano/2539153/commentary/">Commentary</a>
    </li>
    <li class=""><a href="/matches/2018/05/20/italy/serie-a/ss-lazio-roma/fc-internazionale-milano/2539153/venue/">Venue</a>
    </li>
    <li class="">
        <a href="/matches/2018/05/20/italy/serie-a/ss-lazio-roma/fc-internazionale-milano/2539153/map/">Map</a>
    </li>
</ul>

I want get the a tag that contains as InnerText: "Venue". Actually my idea is this:

var nodes = nodes.SelectNodes("//li//a");
HtmlNode a;

foreach(var node in nodes)
{
   if(node.InnerText.ToLower().Contains("venue"))
   {
       a = node;
       break;
   }
}

this is code working but exist another way maybe using xpath or something like?


Solution

  • This will return all anchor tags that contain the word "Venue" //a[contains(text(),'Venue')]