Search code examples
c#.net.net-corewebautomationpuppeteer-sharp

How to get text out of ElementHandle?


Using PuppeteerSharp, I am trying to get the text of the element.

ElementHandle elementHandle = await page.XPathAsync("//html/body/div[1]/section/div/section/h2")[0];

Now that I have the element handle, how do I actually get the text from it? I don't see any obvious methods. I would have expected TextAsync or something similar, but I don't see it.

Using PuppeteerSharp 5.0.


Solution

  • You can call EvaluateFunction passing that ElementHandle as an argument

    var content = await Page.EvaluateFunctionAsync<string>("e => e.textContent", elementHandle);
    

    If you have many scenarios like that, you can build an extension method to solve that for you ;)