Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Headless Chrome .NET API

License

NotificationsYou must be signed in to change notification settings

hardkoded/puppeteer-sharp

NuGetBuild statusDemo build statusCodeFactorBackers

Puppeteer Sharp is a .NET port of the officialNode.JS Puppeteer API.

Recent news

PuppeteerSharp now supports AOT compilation! Check thePuppeteerSharp 19 release notes!.

Useful links

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 agreat 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:

Contributing Guide

Seethis document for information on how to contribute.

Usage

Take screenshots

varbrowserFetcher=newBrowserFetcher();awaitbrowserFetcher.DownloadAsync();awaitusingvarbrowser=awaitPuppeteer.LaunchAsync(newLaunchOptions{Headless=true});awaitusingvarpage=awaitbrowser.NewPageAsync();awaitpage.GoToAsync("http://www.google.com");awaitpage.ScreenshotAsync(outputFile);

snippet source |anchor

You can also change the view port before generating the screenshot

awaitPage.SetViewportAsync(newViewPortOptions{Width=500,Height=500});

snippet source |anchor

Generate PDF files

varbrowserFetcher=newBrowserFetcher();awaitbrowserFetcher.DownloadAsync();awaitusingvarbrowser=awaitPuppeteer.LaunchAsync(newLaunchOptions{Headless=true});awaitusingvarpage=awaitbrowser.NewPageAsync();awaitpage.GoToAsync("http://www.google.com");// In case of fonts being loaded from a CDN, use WaitUntilNavigation.Networkidle0 as a second param.awaitpage.EvaluateExpressionHandleAsync("document.fonts.ready");// Wait for fonts to be loaded. Omitting this might result in no text rendered in pdf.awaitpage.PdfAsync(outputFile);

snippet source |anchor

Inject HTML

awaitusingvarpage=awaitbrowser.NewPageAsync();awaitpage.SetContentAsync("<div>My Receipt</div>");varresult=awaitpage.GetContentAsync();

snippet source |anchor

Evaluate Javascript

awaitusingvarpage=awaitbrowser.NewPageAsync();varseven=awaitpage.EvaluateExpressionAsync<int>("4 + 3");varsomeObject=awaitpage.EvaluateFunctionAsync<JsonElement>("(value) => ({a: value})",5);Console.WriteLine(someObject.GetProperty("a").GetString());

snippet source |anchor

Wait For Selector

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

Wait For Function

using(varpage=awaitbrowser.NewPageAsync()){awaitpage.GoToAsync("http://www.spapage.com");varwatchDog=page.WaitForFunctionAsync("()=> window.innerWidth < 100");awaitpage.SetViewportAsync(newViewPortOptions{Width=50,Height=50});awaitwatchDog;}

Connect to a remote browser

varoptions=newConnectOptions(){BrowserWSEndpoint=$"wss://www.externalbrowser.io?token={apikey}"};varurl="https://www.google.com/";using(varbrowser=awaitPuppeteerSharp.Puppeteer.ConnectAsync(options)){using(varpage=awaitbrowser.NewPageAsync()){awaitpage.GoToAsync(url);awaitpage.PdfAsync("wot.pdf");}}

Sponsors

A massive thanks toJetBrains for a community Resharper and Rider license to use on this project.

And a huge thanks to everyone who sponsors this project throughGithub sponsors:

User avatar: Tolga BalciUser avatar: Richard Garside

Sponsor this project

 

Packages

No packages published

Contributors84


[8]ページ先頭

©2009-2025 Movatter.jp