2

My problem is: Iam trying to conext to http://example.com con Puppeteer Sharp and I want to show in the Console the text of de h1 tag from that page. The text is "Example Domain".

I have this code:

  await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
        var browser = await Puppeteer.LaunchAsync(new LaunchOptions
        {
            Headless = true
        });

        using (var page = await browser.NewPageAsync())
        {
            await page.GoToAsync("http://example.com");
            await page.WaitForSelectorAsync("h1");
            var texto = await page.QuerySelectorAsync("h1");
            Console.WriteLine(texto.ToString());                
        }
        await browser.CloseAsync();

but the Console shows "JSHandle@node". How Can I take the value of the h1 from the JSHandle@node?

Thank you a lot.

1 Answers1

2

You're almost there!

You need a EvaluateFunctionAsync<>() returning the innerText of the H1 after the QuerySelectorAsync()

var texto = await page.QuerySelectorAsync("h1").EvaluateFunctionAsync<string>("_ => _.innerText");
ChristianLD
  • 21
  • 1
  • 3