项目作者: kblok

项目描述 :
无头Chrome .NET API
高级语言: C#
项目地址: git://github.com/kblok/puppeteer-sharp.git
创建时间: 2017-09-30T20:32:25Z
项目社区:https://github.com/kblok/puppeteer-sharp

开源协议:MIT License

下载


Puppeteer Sharp

NuGet
Build status
Demo build status
CodeFactor
Backers

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

Recent news

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

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:

Contributing Guide

See this document for information on how to contribute.

Usage

Take screenshots

  1. var browserFetcher = new BrowserFetcher();
  2. await browserFetcher.DownloadAsync();
  3. await using var browser = await Puppeteer.LaunchAsync(
  4. new LaunchOptions { Headless = true });
  5. await using var page = await browser.NewPageAsync();
  6. await page.GoToAsync("http://www.google.com");
  7. await page.ScreenshotAsync(outputFile);

snippet source | anchor

You can also change the view port before generating the screenshot

  1. await Page.SetViewportAsync(new ViewPortOptions
  2. {
  3. Width = 500,
  4. Height = 500
  5. });

snippet source | anchor

Generate PDF files

  1. var browserFetcher = new BrowserFetcher();
  2. await browserFetcher.DownloadAsync();
  3. await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
  4. await using var page = await browser.NewPageAsync();
  5. await page.GoToAsync("http://www.google.com"); // In case of fonts being loaded from a CDN, use WaitUntilNavigation.Networkidle0 as a second param.
  6. await page.EvaluateExpressionHandleAsync("document.fonts.ready"); // Wait for fonts to be loaded. Omitting this might result in no text rendered in pdf.
  7. await page.PdfAsync(outputFile);

snippet source | anchor

Inject HTML

  1. await using var page = await browser.NewPageAsync();
  2. await page.SetContentAsync("<div>My Receipt</div>");
  3. var result = await page.GetContentAsync();

snippet source | anchor

Evaluate Javascript

  1. await using var page = await browser.NewPageAsync();
  2. var seven = await page.EvaluateExpressionAsync<int>("4 + 3");
  3. var someObject = await page.EvaluateFunctionAsync<JsonElement>("(value) => ({a: value})", 5);
  4. Console.WriteLine(someObject.GetProperty("a").GetString());

snippet source | anchor

Wait For Selector

  1. using (var page = await browser.NewPageAsync())
  2. {
  3. await page.GoToAsync("http://www.spapage.com");
  4. await page.WaitForSelectorAsync("div.main-content")
  5. await page.PdfAsync(outputFile));
  6. }

Wait For Function

  1. using (var page = await browser.NewPageAsync())
  2. {
  3. await page.GoToAsync("http://www.spapage.com");
  4. var watchDog = page.WaitForFunctionAsync("()=> window.innerWidth < 100");
  5. await page.SetViewportAsync(new ViewPortOptions { Width = 50, Height = 50 });
  6. await watchDog;
  7. }

Connect to a remote browser

  1. var options = new ConnectOptions()
  2. {
  3. BrowserWSEndpoint = $"wss://www.externalbrowser.io?token={apikey}"
  4. };
  5. var url = "https://www.google.com/";
  6. using (var browser = await PuppeteerSharp.Puppeteer.ConnectAsync(options))
  7. {
  8. using (var page = await browser.NewPageAsync())
  9. {
  10. await page.GoToAsync(url);
  11. await page.PdfAsync("wot.pdf");
  12. }
  13. }

Sponsors

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




And a huge thanks to everyone who sponsors this project through Github sponsors:

User avatar: Tolga BalciUser avatar: Richard Garside