Skip to content

shekky/puppeteer-sharp

 
 

Repository files navigation

Puppeteer Sharp

NuGet Build status Demo build status

Puppeteer Sharp is a .NET port of the official Node.JS Puppeteer API.

Usage

Take screenshots

await Downloader.CreateDefault().DownloadRevisionAsync(chromiumRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true
}, chromiumRevision);
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.ScreenshotAsync(outputFile);

You can also change the view port before generating the screenshot

await page.SetViewport(new ViewPortOptions
{
    Width = 500,
    Height = 500
});

Generate PDF files

await Downloader.CreateDefault().DownloadRevisionAsync(chromiumRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true
}, chromiumRevision);
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.PdfAsync(outputFile);

Inject HTML

using(var page = await Browser.NewPageAsync())
{
    await page.SetContentAsync("<div>My Receipt</div>");
    var result = await page.GetContentAsync();
    await page.PdfAsync(outputFile);
    SaveHtmlToDB(result);
}

Evaluate Javascript

using (var page = await Browser.NewPageAsync())
{
    var seven = await page.EvaluateFunctionAsync<int>(4 + 3);
    var someObject = await page.EvaluateFunctionAsync<dynamic>("(value) => ({a: value})", 5);
    Console.WriteLine(someObject.a);
}

Wait For Selector

using (var page = await Browser.NewPageAsync())
{
    await page.GoToAsync("http://www.spapage.com");
    await page.WaitForSelectorAsync("div.main-content")
    await page.PdfAsync(outputFile));
}

Wait For Function

using (var page = await Browser.NewPageAsync())
{
    await page.GoToAsync("http://www.spapage.com");
    var watchDog = page.WaitForFunctionAsync("window.innerWidth < 100");
    await Page.SetViewport(new ViewPortOptions { Width = 50, Height = 50 });
    await watchDog;
}

Connect to a remote browser

var options = new ConnectOptions()
{
    BrowserWSEndpoint = $"wss://www.externalbrowser.io?token={apikey}"
};

var url = "https://www.google.com/";

using (var browser = await PuppeteerSharp.Puppeteer.ConnectAsync(options))
{
    using (var page = await browser.NewPageAsync())
    {
        await page.GoToAsync(url);
        await page.PdfAsync("wot.pdf");
    }
}

Monthly reports

Progress

  • Tests on Google's Puppeteer: 405.
  • Tests on Puppeteer Sharp: 322.
  • Passing tests: 319.

Useful links

Packages

No packages published

Languages

  • C# 97.7%
  • HTML 2.0%
  • Other 0.3%