More interactive samples: PuppeteerSharp
Antoine
Posted on May 23, 2021
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"
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
});
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");
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);
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}");
}
}
Hope this helps !
💖 💪 🙅 🚩
Antoine
Posted on May 23, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
githubcopilot AI Innovations at Microsoft Ignite 2024 What You Need to Know (Part 2)
November 29, 2024