ASP.net / HTMLAgilityPack : Getting Value of span within div

asp.net html-agility-pack

Question

Dim myHtml As New HtmlWeb
    Dim myPage As HtmlDocument = myHtml.Load("http://www.mysite.com")
    Dim myNode As HtmlAgilityPack.HtmlNode

    myNode = myPage.DocumentNode.SelectSingleNode("//div[@id='olpDivId']")

Inside of olpDivid there are there spans with identical classes

<span class="blah><a href="fsdfs1>1</a></span>
<span class="blah><a href="fsdfs1>2</a></span>
<span class="blah><a href="fsdfs1>3</a></span>

The problem is that inside of my olpDivId there are three spans with identical classes, and I need to get the text from inside the second one.

Accepted Answer

I think you are looking for this.

var myNode = myPage.DocumentNode.SelectSingleNode("//div[@id='olpDivId']/span[2]/a");

if (myNode != null)
{
    string value = myNode.InnerText.Trim();
}

I tested this code with the below html snippet

<div id="olpDivId">
    <span class="blah"><a href="fsdfs1">1</a></span> <span class="blah"><a href="fsdfs1">
        2</a></span> <span class="blah"><a href="fsdfs1">3</a></span>
</div>



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why