C# Automation with PuppeteerSharp


With PuppeteerSharp you can use Puppeteer together with .NET
PuppeteerSharp is a NetStandard 2.0 library.


The minimum platform versions are .NET Framework 4.6.1 and .NET Core 2.0. The minimum Windows versions supporting the WebSocket library are Windows 8 and Windows Server 2012.

Getting Started

Let's create a basic project:


dotnet new console -n headless_puppeteer_demo

Now, we need to add the PuppeteerSharp dependency:


dotnet add package PuppeteerSharp

Let's create a sample script in Program.cs which will navigate to our website and take a screenshot:


using System;
using System.Threading.Tasks;
using PuppeteerSharp;

namespace _headless_puppeteer_demo
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var options = new ConnectOptions()
            {
                BrowserWSEndpoint = "wss://chrome.headlesstesting.com?token=[YOUR-TOKEN]",
            };

            using (var browser = await PuppeteerSharp.Puppeteer.ConnectAsync(options))
            {
                using (var page = await browser.NewPageAsync())
                {
                    await page.GoToAsync("https://headlesstesting.com");
                    await page.ScreenshotAsync("screenshot.jpg");
                    await page.CloseAsync();
                }
            }            
        }
    }
}

Next, we'll need to build the project:


dotnet build

And finally, we can run the script:


dotnet run

This will open a Headless Browser on our service, take a screenshot and save the screenshot on your filesystem as screenshot.jpg.