What are the event listeners available in Puppeteer-Sharp?

Puppeteer-Sharp is a .NET port of the Node library Puppeteer which provides a high-level API over the Chrome DevTools Protocol. Puppeteer-Sharp is used for automating and controlling a headless Chrome or Chromium browser.

In Puppeteer-Sharp, you can listen to various browser-related events, which are useful for handling different aspects of page navigation and interaction. Event listeners in Puppeteer-Sharp are generally available on the Page, Browser, and BrowserContext classes, among others.

Here are some of the commonly used event listeners:

On the Page Class

  • Page.Dialog: Fired when a dialog appears (alert, prompt, confirm, or beforeunload).
  • Page.Console: Fired when a console message is emitted within the page.
  • Page.Load: Emitted when the load event is dispatched on the page.
  • Page.DOMContentLoaded: Fired when the DOMContentLoaded event is dispatched.
  • Page.Error: Fired when an uncaught exception happens within the page.
  • Page.Request: Emitted when a request is issued from the page.
  • Page.Response: Emitted when a response is received for a request.
  • Page.RequestFailed: Emitted when a request fails.
  • Page.RequestFinished: Emitted when a request finishes successfully.
  • Page.FrameAttached: Fired when a frame is attached to the page.
  • Page.FrameDetached: Fired when a frame is detached from the page.
  • Page.FrameNavigated: Fired when a frame navigates to a new URL.

On the Browser Class

  • Browser.TargetCreated: Emitted when a new page target is created.
  • Browser.TargetDestroyed: Emitted when a page target is destroyed.
  • Browser.TargetChanged: Emitted when the URL of a target changes.
  • Browser.Disconnected: Fired when the browser gets disconnected from the Chromium instance.

On the BrowserContext Class

  • BrowserContext.TargetCreated: Similar to Browser.TargetCreated, but for a specific browser context.
  • BrowserContext.TargetDestroyed: Similar to Browser.TargetDestroyed, but for a specific browser context.
  • BrowserContext.TargetChanged: Similar to Browser.TargetChanged, but for a specific browser context.

Example Usage

Here's an example of how you might use some of these event listeners in a Puppeteer-Sharp script:

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

class Program
{
    public static async Task Main()
    {
        await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
        var browser = await Puppeteer.LaunchAsync(new LaunchOptions
        {
            Headless = true
        });

        var page = await browser.NewPageAsync();

        // Listen for console messages
        page.Console += (sender, e) => Console.WriteLine($"CONSOLE: {e.Message.Text}");

        // Listen for requests
        page.Request += (sender, e) => Console.WriteLine($"REQUEST: {e.Request.Url}");

        // Listen for responses
        page.Response += (sender, e) => Console.WriteLine($"RESPONSE: {e.Response.Url} - {e.Response.Status}");

        // Navigate to a page
        await page.GoToAsync("http://example.com");

        // Close the browser
        await browser.CloseAsync();
    }
}

In the example above, we attach event handlers to the Console, Request, and Response events to log information to the console. The script sets up a headless browser, creates a new page, and navigates to http://example.com, all the while listening for the specified events.

Please note that event names in Puppeteer-Sharp are members of corresponding classes and are used as event properties rather than strings, which is different from the JavaScript version of Puppeteer.

Related Questions

Get Started Now

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