Headless Chrome .NET API
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
appveyor Examples section on the site (#725) Oct 31, 2018
demos We don't need no dynamics (#823) Dec 31, 2018
docfx_project Add Linux info to the readme doc (#855) Jan 26, 2019
lib Sync WaitForSelectorOrXPathAsync code (#929) Feb 22, 2019
samples HOWOT: Reuse chrome (#789) Dec 10, 2018
.editorconfig Add editorconfig (#328) Jun 16, 2018
.gitignore AspNet WebSocket Transport (#776) Dec 6, 2018
CODE_OF_CONDUCT.md Add CODE_OF_CONDUCT.md Mar 10, 2018
CONTRIBUTING.md Docs: SSL Server on Mac Jun 14, 2018
ISSUE_TEMPLATE.md
LICENSE Initial commit Sep 30, 2017
README.md Add reports + backers (#911) Feb 8, 2019
appveyor-demo.yml Upgrade .NET Core projects to 2.2 (#821) Dec 27, 2018
appveyor-fullframework.yml Fix AspNetFramework package build (#802) Dec 17, 2018
appveyor-linux.yml Upgrade .NET Core projects to 2.2 (#821) Dec 27, 2018
appveyor.yml Upgrade .NET Core projects to 2.2 (#821) Dec 27, 2018

README.md

Puppeteer Sharp

NuGet Build status Demo build status CodeFactor Backers

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

Useful links

Prerequisites

  • As Puppeteer-Sharp is a NetStandard 2.0 library, The minimum platform versions are .NET Framework 4.6.1 and .NET Core 2.0. Read more.
  • The minimum Windows versions supporting the WebSocket library are Windows 8 and Windows Server 2012. Read more.
  • Mono is required on Linux. Read more about installing Mono here.
  • If you have issues running Chrome on Linux, the Puppeteer repo has a great troubleshooting guide.

How to Contribute and Provide Feedback

Some of the best ways to contribute are to try things out file bugs and fix issues.

If you have an issue or a question:

Contributing Guide

See this document for information on how to contribute.

Usage

Take screenshots

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true
});
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.SetViewportAsync(new ViewPortOptions
{
    Width = 500,
    Height = 500
});

Generate PDF files

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true
});
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.SetViewportAsync(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

Backers

Support us with a monthly donation and help us continue our activities. Become a backer.

Thanks

Thanks to JetBrains for a community Resharper license to use on this project.