playwright-go

repository·main·Indexed 25 days ago

https://github.com/mxschmitt/playwright-go

A Go library providing a single API to automate Chromium, Firefox, and WebKit browsers for reliable, fast, and cross-browser web automation. It features multi-context automation, auto-waiting, network interception, emulation, Shadow DOM support, and native input. The library includes tools for managing browser contexts, cookies, permissions, geolocation, and storage state, as well as a comprehensive assertions engine for Pages, Locators, and API responses.

Tokens
29.8K
Snippets
25
Records
237
Agent score
83%

What's inside playwright-go

  1. Playwright capabilities

    main

    Playwright for Go provides a wide range of web automation capabilities, including:

    • Multi-context automation: Scenarios spanning multiple pages, domains, and iframes.
    • Auto-waiting: Automatically waits for elements to be ready before performing actions like click or fill.
    • Network Interception: Intercept network activity for stubbing and mocking requests.
    • Emulation: Emulate mobile devices, geolocation, and permissions.
    • Shadow DOM support: Support for web components via shadow-piercing selectors.
    • Native Input: Support for native mouse and keyboard events.
    • File Handling: Support for uploading and downloading files.
  2. Install playwright-go

    main

    To use playwright-go, first add the package to your Go module:

    go get -u github.com/mxschmitt/playwright-go

    Next, you must install the Playwright driver and the required browsers. Important: Replace 0.xxxx.x with the exact version used in your go.mod file, as each minor version requires a specific driver version.

    You can use the playwright command to install the driver and browsers. Use the --with-deps flag to automatically install necessary OS dependencies.

    go run github.com/mxschmitt/playwright-go/cmd/playwright@v0.xxxx.x install --with-deps
    # Or, if you have the playwright command installed:
    playwright install --with-deps

    Option 2: Programmatic Installation

    You can trigger the installation directly from your Go code. Note that if your OS lacks the required browser dependencies, you will still need to install them manually using elevated privileges.

    go get -u github.com/mxschmitt/playwright-go
    
    go run github.com/mxschmitt/playwright-go/cmd/playwright@v0.xxxx.x install --with-deps
  3. Interact with the Page object

    main
    The Page object represents a single browser tab. It provides a high-level API for interacting with the web content, including navigation, element interaction, and script execution. Most methods on Page are proxies to the MainFrame.
  4. Start a Playwright instance with Run()

    main

    Use Run to start a Playwright instance. This function requires that the driver and browsers are already installed (via Install() or the Playwright CLI). It returns a *Playwright instance which is the entry point for your automation scripts.

    playwright, err := playwright.Run(&playwright.RunOptions{
        // Optional configuration
    })
    if err != nil {
        log.Fatal(err)
    }
    // Use playwright instance...
  5. Install the Playwright driver and browsers

    main

    Use the Install function to download the Playwright driver and the necessary browser binaries. This must be called before attempting to run Playwright. You can pass RunOptions to customize the installation (e.g., specifying which browsers to install or providing a custom driver directory).

    err := playwright.Install(&playwright.RunOptions{
        Browsers: []string{"chromium"},
        WithDeps: true,
    })
    if err != nil {
        log.Fatal(err)
    }
  6. Basic usage example: Scrape Hacker News

    main

    This example demonstrates how to initialize Playwright, launch a Chromium browser, navigate to a page, and extract text content from specific elements using locators.

    package main
    
    import (
    	"fmt"
    	"log"
    
    	"github.com/mxschmitt/playwright-go"
    )
    
    func main() {
    	pw, err := playwright.Run()
    	if err != nil {
    		log.Fatalf("could not start playwright: %v", err)
    	}
    	browser, err := pw.Chromium.Launch()
    	if err != nil {
    		log.Fatalf("could not launch browser: %v", err)
    	}
    	page, err := browser.NewPage()
    	if err != nil {
    		log.Fatalf("could not create page: %v", err)
    	}
    	if _, err = page.Goto("https://news.ycombinator.com"); err != nil {
    		log.Fatalf("could not goto: %v", err)
    	}
    	entries, err := page.Locator(".athing").All()
    	if err != nil {
    		log.Fatalf("could not get entries: %v", err)
    	}
    	for i, entry := range entries {
    		title, err := entry.Locator("td.title > span > a").TextContent()
    		if err != nil {
    			log.Fatalf("could not get text content: %v", err)
    		}
    		fmt.Printf("%d: %s\n", i+1, title)
    	}
    	if err = browser.Close(); err != nil {
    		log.Fatalf("could not close browser: %v", err)
    	}
    	if err = pw.Stop(); err != nil {
    		log.Fatalf("could not stop Playwright: %v", err)
    	}
    }
  7. Configure RunOptions for driver and browser installation

    main

    The RunOptions struct allows you to customize how the Playwright driver and browsers are managed. Key options include:

    • DriverDirectory: Path to the driver directory. Can also be set via PLAYWRIGHT_DRIVER_PATH env var.
    • Browsers: A slice of strings specifying which browsers to install (e.g., []string{"chromium", "firefox"}). If not set, all browsers are downloaded.
    • SkipInstallBrowsers: If true, only the driver is installed, skipping browser downloads.
    • OnlyInstallShell: Only downloads the headless shell (Chromium only).
    • NoInstallShell: Does not install the Chromium headless shell.
    • WithDeps: Installs system dependencies for browsers.
    • DryRun: Prints information without actually installing browsers or dependencies.
    • Verbose: Enables verbose logging (default is true).
    • Stdout / Stderr: Custom writers for standard output and error streams.
  8. Handle errors in Locators

    main

    Locators in playwright-go use lazy error handling. Errors encountered during locator construction (like using a locator from a different frame) are stored and returned when an action method is called.

    Use the Err() error method to check if a locator is in an invalid state before performing actions.

    Common Error:

    • ErrLocatorNotSameFrame: Occurs when using And, Or, Has, or HasNot with locators belonging to different frames.
  9. Initialize and run the Playwright driver via CLI

    main

    To create a custom CLI entrypoint that manages the Playwright driver lifecycle, you can use playwright.NewDriver with RunOptions. The process involves:

    1. Starting the driver using playwright.NewDriver.
    2. Downloading the necessary driver binaries via driver.DownloadDriver().
    3. Executing commands passed through CLI arguments using driver.Command(os.Args[1:]...).

    This pattern allows you to wrap the Playwright driver and expose its command-line interface directly through your own Go application.

    package main
    
    import (
    	"log"
    	"os"
    
    	"github.com/mxschmitt/playwright-go"
    )
    
    func main() {
    	driver, err := playwright.NewDriver(&playwright.RunOptions{})
    	if err != nil {
    		log.Fatalf("could not start driver: %v", err)
    	}
    	if err = driver.DownloadDriver(); err != nil {
    		log.Fatalf("could not download driver: %v", err)
    	}
    	cmd := driver.Command(os.Args[1:]...)
    	cmd.Stdout = os.Stdout
    	cmd.Stderr = os.Stderr
    	if err := cmd.Run(); err != nil {
    		log.Fatalf("could not run driver: %v", err)
    	}
    	os.Exit(cmd.ProcessState.ExitCode())
    }
  10. Listen to page events

    main

    The Page object allows you to register callbacks for various lifecycle and network events using On* methods:

    • OnClose(fn func(Page)): Triggered when the page is closed.
    • OnConsole(fn func(ConsoleMessage)): Triggered when a console message is emitted.
    • OnCrash(fn func(Page)): Triggered when the page crashes.
    • OnDialog(fn func(Dialog)): Triggered when a dialog (alert, confirm, etc.) appears.
    • OnDOMContentLoaded(fn func(Page)): Triggered when the DOM is fully loaded.
    • OnDownload(fn func(Download)): Triggered when a download starts.
    • OnFileChooser(fn func(FileChooser)): Triggered when a file chooser is opened.
    • OnFrameAttached(fn func(Frame)): Triggered when a frame is attached.
    • OnFrameDetached(fn func(Frame)): Triggered when a frame is detached.
    • OnFrameNavigated(fn func(Frame)): Triggered when a frame navigates.
    • OnLoad(fn func(Page)): Triggered when the page has loaded.
    • OnPageError(fn func(error)): Triggered when a page error occurs.
    • OnPopup(fn func(Page)): Triggered when a popup is created.
    • OnRequest(fn func(Request)): Triggered when a request is made.
    • OnRequestFailed(fn func(Request)): Triggered when a request fails.
    • OnRequestFinished(fn func(Request)): Triggered when a request finishes.
    • OnResponse(fn func(Response)): Triggered when a response is received.
    • OnWebSocket(fn func(WebSocket)): Triggered when a WebSocket is created.
    • OnWorker(fn func(Worker)): Triggered when a worker is created.
  11. Configure Permissions and Geolocation

    main

    Control browser permissions and location settings for the context:

    • GrantPermissions(permissions []string, options ...BrowserContextGrantPermissionsOptions) error: Grants specific permissions (e.g., "geolocation", "notifications").
    • ClearPermissions() error: Removes all granted permissions.
    • SetGeolocation(geolocation *Geolocation) error: Sets the geolocation for the context.
    • ResetGeolocation() error: Resets geolocation to default.