Is Puppeteer-Sharp compatible with .NET Core and .NET Framework?

Yes, Puppeteer-Sharp is fully compatible with both .NET Core and .NET Framework. It's a .NET port of the popular Node.js Puppeteer library, providing a high-level API for controlling headless Chrome browsers through the Chrome DevTools Protocol.

Framework Compatibility

Puppeteer-Sharp targets .NET Standard 2.0, which ensures broad compatibility across the .NET ecosystem:

Primary Targets

  • .NET Core 2.0+ (including .NET 5, 6, 7, 8+)
  • .NET Framework 4.6.1+

Additional Platforms

  • Mono 5.4+
  • Xamarin.iOS 10.14+
  • Xamarin.Mac 3.8+
  • Xamarin.Android 8.0+
  • Universal Windows Platform (UWP) 10.0.16299+

Installation

Using Package Manager Console

Install-Package PuppeteerSharp

Using .NET CLI

dotnet add package PuppeteerSharp

Using PackageReference (in .csproj)

<PackageReference Include="PuppeteerSharp" Version="*" />

Basic Usage Example

Here's a comprehensive example demonstrating Puppeteer-Sharp usage:

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

class Program
{
    public static async Task Main(string[] args)
    {
        // Download Chromium if not already present
        await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

        // Launch browser with options
        using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
        {
            Headless = true,
            Args = new[] { "--no-sandbox", "--disable-setuid-sandbox" }
        });

        // Create new page
        using var page = await browser.NewPageAsync();

        // Set viewport size
        await page.SetViewportAsync(new ViewPortOptions
        {
            Width = 1920,
            Height = 1080
        });

        // Navigate to website
        await page.GoToAsync("https://example.com", WaitUntilNavigation.Networkidle2);

        // Take screenshot
        await page.ScreenshotAsync("example.png");

        // Generate PDF
        await page.PdfAsync("example.pdf", new PdfOptions
        {
            Format = PaperFormat.A4,
            PrintBackground = true
        });

        Console.WriteLine("Screenshot and PDF generated successfully!");
    }
}

Advanced Example: Web Scraping

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

public class WebScraper
{
    public static async Task Main()
    {
        await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

        using var browser = await Puppeteer.LaunchAsync(new LaunchOptions 
        { 
            Headless = true 
        });

        using var page = await browser.NewPageAsync();

        // Navigate and wait for content
        await page.GoToAsync("https://news.ycombinator.com");
        await page.WaitForSelectorAsync(".titleline");

        // Extract data
        var titles = await page.EvaluateExpressionAsync<string[]>(@"
            Array.from(document.querySelectorAll('.titleline > a'))
                 .map(link => link.textContent.trim())
        ");

        Console.WriteLine("Top Stories:");
        foreach (var title in titles.Take(5))
        {
            Console.WriteLine($"- {title}");
        }
    }
}

Common Use Cases

Puppeteer-Sharp excels in various automation scenarios:

  • PDF Generation from HTML content
  • Screenshot Capture for testing or documentation
  • Web Scraping of dynamic content
  • Form Automation and testing
  • Performance Monitoring and analysis
  • Automated Testing of web applications

Project Configuration

For .NET Framework Projects

Add the package reference in packages.config:

<package id="PuppeteerSharp" version="latest" targetFramework="net461" />

For .NET Core/.NET 5+ Projects

The package works seamlessly with modern .NET project files and dependency injection frameworks.

Conclusion

Puppeteer-Sharp provides excellent cross-platform compatibility, making it an ideal choice for .NET developers who need reliable browser automation capabilities across different .NET implementations. Its .NET Standard 2.0 target ensures maximum compatibility while maintaining all the powerful features of the original Puppeteer library.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon