Puppeteer Sharp
Headless Chrome .NET API
Install / Use
npx skills add hardkoded/puppeteer-sharpInstalls into whichever agent you are using.
README
Puppeteer Sharp
Puppeteer Sharp is a .NET port of the official Node.JS Puppeteer API.
Recent news
Test the latest Firefox versions using WebDriver BiDi! Check the PuppeteerSharp 21 release notes.
PuppeteerSharp now supports AOT compilation! Check the PuppeteerSharp 19 release notes!.
Useful links
- API Documentation
- Slack channel #puppeteer-sharp
- StackOverflow
- Issues
- Blog
Published Packages
| Package | Description | | ------- | ----------- | | PuppeteerSharp | Our full cross-browser automation tool | | PuppeteerSharp.Cdp | Ideal for Chrome-only AOT apps where binary size matters | | PuppeteerSharp.AspNetFramework | The companion library you need to run PuppeteerSharp in ASP.NET Classic |
Prerequisites
- Puppeteer-Sharp comes in two flavors: a NetStandard 2.0 library for .NET Framework 4.6.1 and .NET Core 2.0 or greater and a .NET 8 version.
- If you have issues running Chrome on Linux, the Puppeteer repo has a great troubleshooting guide.
- X-server is required on Linux.
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:
- Ask a question on Stack Overflow.
- File a new issue.
Contributing Guide
See this document for information on how to contribute.
Usage
Take screenshots
<!-- snippet: screenshotasync_example --><a id='snippet-screenshotasync_example'></a>
var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();
await using var browser = await Puppeteer.LaunchAsync(
new LaunchOptions { Headless = true });
await using var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.ScreenshotAsync(outputFile);
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/ScreenshotTests/PageScreenshotTests.cs#L54-L62' title='Snippet source file'>snippet source</a> | <a href='#snippet-screenshotasync_example' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->You can also change the view port before generating the screenshot
<!-- snippet: setviewportasync_example --><a id='snippet-setviewportasync_example'></a>
await Page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/ScreenshotTests/ElementHandleScreenshotTests.cs#L12-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-setviewportasync_example' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->Generate PDF files
<!-- snippet: pdfasync_example --><a id='snippet-pdfasync_example'></a>
var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
await using var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com"); // In case of fonts being loaded from a CDN, use WaitUntilNavigation.Networkidle0 as a second param.
await page.EvaluateExpressionHandleAsync("document.fonts.ready"); // Wait for fonts to be loaded. Omitting this might result in no text rendered in pdf.
await page.PdfAsync(outputFile);
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/PageTests/PdfTests.cs#L24-L34' title='Snippet source file'>snippet source</a> | <a href='#snippet-pdfasync_example' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->Inject HTML
<!-- snippet: setcontentasync_example --><a id='snippet-setcontentasync_example'></a>
await using var page = await browser.NewPageAsync();
await page.SetContentAsync("<div>My Receipt</div>");
var result = await page.GetContentAsync();
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/PageTests/SetContentTests.cs#L14-L20' title='Snippet source file'>snippet source</a> | <a href='#snippet-setcontentasync_example' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->Evaluate Javascript
<!-- snippet: evaluate_example --><a id='snippet-evaluate_example'></a>
await using var page = await browser.NewPageAsync();
var seven = await page.EvaluateExpressionAsync<int>("4 + 3");
var someObject = await page.EvaluateFunctionAsync<JsonElement>("(value) => ({a: value})", 5);
Console.WriteLine(someObject.GetProperty("a").GetString());
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/QuerySelectorTests/ElementHandleQuerySelectorEvalTests.cs#L17-L22' title='Snippet source file'>snippet source</a> | <a href='#snippet-evaluate_example' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->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");
}
}
Sponsors
A massive thanks to JetBrains for a community Resharper and Rider license to use on this project.
<div style="display:inline"> </div>And a huge thanks to everyone who sponsors this project through Github sponsors:
<!-- sponsors --><a href="https://github.com/tolgabalci"><img src="https://github.com/tolgabalci.png" width="60px" alt="User avatar: Tolga Balci" /></a><a href="https://github.com/nogginbox"><img src="https://github.com/nogginbox.png" width="60px" alt="User avatar: Richard Garside" /></a><a href="https://github.com/htmlcsstoimage"><img src="https://github.com/htmlcsstoimage.png" width="60px" alt="User avatar: HTML/CSS to Image API" /></a><a href="https://github.com/iron-software"><img src="https://github.com/iron-software.png" width="60px" alt="User avatar: Iron Software" /></a><!-- sponsors -->Related Skills
gh-issues
385.5kFetch GitHub issues, select candidates, spawn background fix agents, open PRs, and optionally process PR review comments.
node-connect
385.5kDiagnose OpenClaw Android, iOS, or macOS node pairing, QR/setup code, route, auth, and connection failures.
browser-automation
385.5kUse when controlling web pages with the OpenClaw browser tool, especially multi-step flows, login checks, tab management, or recovery from stale refs/timeouts.
imsg
385.5kUse the imsg CLI from OpenClaw agents for iMessage/SMS DMs, groups, replies, reactions, polls, watching, and private-API actions.
