mary
mary

Reputation: 267

How to access nested div based on class name

I have this html code:

<div class="searchResult webResult">

  <div class="resultTitlePane">
    Google
  </div>

  <div class="resultDisplayUrlPane">
    www.google.com  
  </div>

 <div class="resultDescription">
   Search
 </div>
</div>

I want to access innertext inside divs in diffrent variables
I know for accessing a div with a class I hould write

var titles = hd.DocumentNode.SelectNodes("//div[@class='searchResult webResult']"); 

foreach (HtmlNode node in titles)
    {?}

what code should I write to get the innertext of each dive in different variables.TNX

Upvotes: 1

Views: 1040

Answers (2)

Steve Wellens
Steve Wellens

Reputation: 20620

Since you don't know how many nodes will be returned, I suggest using a list:

List<string> titlesStringList = new List<string>();
foreach (HtmlNode node in titles)
{
    titlesStringList.Add(node.InnerText);
}

Upvotes: 0

alecxe
alecxe

Reputation: 473873

I would extend the current XPath expression you have to match the inner div elements:

//div[@class='searchResult webResult']/div[contains(@class, 'result')]

Then, to get the text, use the .InnerText property:

Upvotes: 2

Related Questions