DEV Community

Cover image for More interactive samples: PuppeteerSharp
Antoine
Antoine

Posted on • Updated on

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 !

Top comments (0)