rod

repository·main·Indexed 27 days ago

https://github.com/go-rod/rod

A high-level Go driver for web automation and scraping built on the Chrome DevTools Protocol (CDP). It features auto-waiting, chained context design for timeouts, and support for nested iframes and Shadow DOMs. The project includes the `lib/cdp` package for direct CDP interaction and `lib/proto` for encoding and decoding CDP protocol data.

Tokens
12.3K
Snippets
8
Records
96
Agent score
91%

What's inside rod

  1. Overview of Rod

    main

    Rod is a high-level web automation and scraping driver for Go, built directly on the Chrome DevTools Protocol. It provides both high-level helpers for common tasks and low-level access for deep customization.

    Key features include:

    • Chained context design: Intuitive way to handle timeouts or cancellations for long-running tasks.
    • Auto-waiting: Automatically waits for elements to be ready before interaction.
    • Robustness: Handles nested iframes and Shadow DOMs correctly, and uses leakless to ensure no zombie browser processes remain after a crash.
    • Debugging: Supports auto input tracing and remote monitoring of headless browsers.
    • Concurrency: All operations are thread-safe.
    • Browser Management: Automatically finds or downloads the required browser via the launcher package.
  2. Use the lib/proto library to encode/decode CDP protocol data

    main
    The lib/proto library is a standalone and stateless tool designed to encode and decode data following the Chrome DevTools Protocol (CDP) format. Because it is stateless, it can be used independently of the main rod library, making it useful for encoding or decoding JSON when working with other browser automation tools that utilize the CDP protocol.
  3. Explore Rod examples and usage

    main

    To learn how to use Rod, you can explore several resources:

    1. examples_test.go: The primary starting point for quick code snippets.
    2. lib/examples folder: A collection of more structured examples.
    3. Unit Tests: For specific method usage (e.g., HandleAuth), search the repository's *_test.go files to see how the methods are implemented and called in real scenarios.
    4. Comparison: If you are coming from chromedp, check lib/examples/compare-chromedp to see the differences in implementation style.
  4. Intercept requests with HijackRequests

    main

    You can intercept network requests at either the Browser level (intercepting all requests from all pages) or the Page level (intercepting requests only for a specific page) using HijackRequests().

    When hijacking is enabled, page caching is disabled, though 304 Not Modified responses will still function.

    To use the router, you must call .Add() to define patterns, and then call .Run() to start the interception process. After calling .Run(), you should not add new handlers to the router.

  5. Use Must-prefixed methods for panic-on-error behavior

    main

    The rod package provides a suite of methods prefixed with Must (e.g., MustClick, MustNavigate). These are wrappers around the standard API methods that automatically trigger a panic if an error is returned. This is useful for simplifying code when you expect operations to succeed and want to avoid repetitive error handling.

    By default, these methods panic. However, you can customize this behavior using WithPanic to define a custom failure function (e.g., for logging or controlled exits).

  6. Expose Go functions to the Page's JavaScript context

    main

    Use ExposeHelpers to inject Go functions into the browser's JavaScript environment. This allows you to call Go code directly from the DevTools console or from within page.Evaluate calls. The functions are attached to a global window.rod object in the browser.

    Pass one or more *js.Function pointers to the method.

  7. Capture Screenshots and PDFs

    main

    Extract visual representations of the page:

    • Screenshot(fullPage bool, req *proto.PageCaptureScreenshot): Captures a screenshot. If fullPage is true, it adjusts the viewport to capture the entire scrollable area.
    • ScrollScreenshot(opt *ScrollScreenshotOptions): Performs a scroll-and-stitch screenshot. This is useful for long pages but may repeat fixed-position elements (like headers). Use FixedTop and FixedBottom to optimize.
    • PDF(req *proto.PagePrintToPDF): Prints the page as a PDF. Returns a *StreamReader.
    • CaptureDOMSnapshot(): Returns a flattened array of the full DOM tree, including iframes and shadow DOM, along with layout and style information.
  8. Insert text into the page

    main
    Use Page.InsertText(text string) to simulate pasting text into the page. This is the recommended method for inputting characters that are not present on a standard keyboard (e.g., CJK characters).
  9. Overlay a message on an Element

    main

    The Overlay method draws a message directly over a specific *Element. This is useful for verifying which element is being targeted by an action.

    It returns a removeOverlay function that clears the overlay from that specific element.

  10. Use WithCancel for manual context cancellation

    main
    The WithCancel() method returns a clone of the *Browser, *Page, or *Element along with a cancel function. Calling the returned cancel() function will cancel the context used by the cloned object and all its subsequent chained operations.
  11. Configure Page Headers and User Agent

    main

    Customize how the page makes network requests:

    • SetExtraHeaders(dict []string): Sets extra HTTP headers to be sent with every request. The input dict should be a slice of strings in [key, value, key, value, ...] format. Returns a func() to clear these headers and an error.
    • SetUserAgent(req *proto.NetworkSetUserAgentOverride): Sets the User Agent. If req is nil, a default laptop Chrome User Agent is used.
    • SetBlockedURLs(urls []string): Blocks specific requests using patterns (wildcards * allowed). An empty pattern "" blocks all requests.