user1265533
user1265533

Reputation: 173

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

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.

Upvotes: 1

Views: 1464

Answers (1)

Anil Vangari
Anil Vangari

Reputation: 570

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>

Upvotes: 1

Related Questions