-1

I am trying to get all the innertext from a list of "li"-elements. It seems I am hitting something, there are 19 elements in the variable has, but I don't know how to pick out the actual innertext values:

string xpath = "//h1[@title='UL']//li";
IElementHandle[] has = await ((IPage)pageTabel).XPathAsync(xp);
IJSHandle ha = has[0].GetPropertiesAsync("value");
Palle Due
  • 5,929
  • 4
  • 17
  • 32

2 Answers2

0

I think e.g.

foreach (var listItem in has)
{
    Console.WriteLine((await listItem.GetPropertyAsync("textContent")).RemoteObject.Value.ToString()); 
}

would work. I don't know whether browser's also implement the (originally IE only) innerText property, if they do then of course above doing GetPropertyAsync("innerText") instead should also work.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

If you'd prefer a strongly typed experience then PuppeteerSharp.Dom provides a set of extensions to PuppeteerSharp.

Install PuppeteerSharp.Dom from Nuget.org then you can use the strongly typed extensions.

// Add using PuppeteerSharp.Dom; to access XPathAsync<T>

string xpath = "//h1[@title='UL']//li";
var has = await ((IPage)pageTabel).XPathAsync<HtmlListItemElement>(xpath);

foreach (var listItem in has)
{
    var textContent = await listItem.GetTextContentAsync();
}
amaitland
  • 4,073
  • 3
  • 25
  • 63