Puppeteer-Sharp is a .NET port of the Node.js library Puppeteer which provides a high-level API to control Chromium or Chrome over the DevTools Protocol. Puppeteer-Sharp is used for browser automation tasks such as taking screenshots, generating PDFs, and performing automated testing.
To run Puppeteer-Sharp, you need the following system requirements:
- .NET Runtime: Puppeteer-Sharp is a .NET library, so you need a compatible .NET runtime. It supports .NET Standard 2.0, which means it should work on the following runtimes:
- .NET Core 2.0 or later
- .NET Framework 4.6.1 or later (Windows only)
- Mono 5.4 or later
Ensure you have one of these runtimes installed on your system.
Operating System: Since Puppeteer-Sharp targets .NET Standard 2.0, it is cross-platform and can run on any operating system that supports the .NET Core runtime, including:
- Windows
- Linux
- macOS
Chromium/Chrome Browser: Puppeteer-Sharp interacts with either Chromium or Chrome browsers. When you install Puppeteer-Sharp via NuGet, it will download a compatible version of Chromium by default. If you prefer to use a specific version of Chrome, you should ensure it is installed on your system.
Hardware: There are no specific hardware requirements for running Puppeteer-Sharp beyond what is required by the .NET runtime and the Chromium/Chrome browser. However, running browser automation tasks can be resource-intensive, so having a sufficient amount of RAM and a decent processor will improve performance.
Dependencies: Puppeteer-Sharp has various dependencies that are managed via NuGet. When you install Puppeteer-Sharp using NuGet, these dependencies will be automatically resolved and installed.
Installation Steps
To install Puppeteer-Sharp, you typically use the NuGet package manager. Here is how you can do it via the command line:
# For .NET Core CLI
dotnet add package PuppeteerSharp
# For NuGet CLI
nuget install PuppeteerSharp
Alternatively, you can use the NuGet Package Manager in Visual Studio or add the package reference directly to your .csproj
file:
<ItemGroup>
<PackageReference Include="PuppeteerSharp" Version="x.x.x" />
</ItemGroup>
Replace x.x.x
with the specific version of Puppeteer-Sharp you want to install.
After installing Puppeteer-Sharp, you can use it in your .NET application to automate browser tasks. Here's a simple example of taking a screenshot of a webpage using Puppeteer-Sharp:
using System;
using System.Threading.Tasks;
using PuppeteerSharp;
class Program
{
public static async Task Main(string[] args)
{
// Download the Chromium browser if it's not already present
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
// Launch the browser
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))
{
// Create a new page
using (var page = await browser.NewPageAsync())
{
// Navigate to the specified URL
await page.GoToAsync("http://example.com");
// Take a screenshot of the page
await page.ScreenshotAsync("example.png");
}
}
Console.WriteLine("Screenshot saved as example.png");
}
}
Make sure you have the necessary permissions to write files to the disk when saving screenshots or PDFs. Also, if you're running on Linux, you might need to install additional dependencies required by Chromium such as libx11
, libxcomposite
, libxdamage
, libxi
, and others.