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 !
Top comments (0)