DEV Community

Cover image for More interactive samples: PuppeteerSharp
Antoine
Antoine

Posted on • Edited on

1 1

More interactive samples: PuppeteerSharp

Photo by Killian Cartignies on Unsplash

Following the notebook that demonstrates how to learn .Net, i'm posting here the sample to play with PuppeteerSharp.

External Reference download

First, let's declare eternal dependencies required, in a dedicated section of code.

#r "nuget:PuppeteerSharp, 4.0.0"
Enter fullscreen mode Exit fullscreen mode

Global context initilization

Then, let's init variable that we will use through the notebook.
It will take some time as a dedicated version of chrome will be downloaded.

using PuppeteerSharp;

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

We will have now a browser initialized.

Play with it

We can now do some basic tests or more complicated ones.

Take screenshot

var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.ScreenshotAsync("./FirstTest.png");
Enter fullscreen mode Exit fullscreen mode

Select content

var currentPage = await browser.NewPageAsync();
await currentPage.GoToAsync("http://www.google.com");
await currentPage.QuerySelectorAllHandleAsync("input[name=q]").EvaluateFunctionAsync("el => el.value = 'test'");
await currentPage.ScreenshotAsync("./screenshot.jpg");
var output = await currentPage.QuerySelectorAllHandleAsync("div[id='SIvCob']").EvaluateFunctionAsync<string[]>("el => el.map(a => a.text)");
display(output);
Enter fullscreen mode Exit fullscreen mode

Note: display method is provided by dotnet interactive.

Iterating urls

using (var page = await browser.NewPageAsync())
{
    await page.GoToAsync("http://www.google.com");
    var jsSelectAllAnchors = @"Array.from(document.querySelectorAll('div')).map(a => a.innerText);";
    var urls = await page.EvaluateExpressionAsync<string[]>(jsSelectAllAnchors);
    foreach (string url in urls)
    {
        Console.WriteLine($"Url: {url}");
    }
}
Enter fullscreen mode Exit fullscreen mode

Hope this helps !

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay