简体   繁体   中英

How to get innertext of all li elements in an list with puppeteer-sharp and XPath in C#

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");

I think eg

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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM