chromote

repository·main·Indexed 41 days ago

https://github.com/rstudio/chromote

An R implementation of the Chrome DevTools Protocol used to programmatically control Chromium-based browsers. It provides the ChromoteSession object for executing protocol methods, navigating URLs via $go_to(), and capturing screenshots via $screenshot(). It serves as a foundation for higher-level packages such as {shinytest2} and {rvest}.

Tokens
558
Snippets
5
Records
5
Agent score
2%

What's inside chromote

  1. Install chromote

    main

    You can install the released version of chromote from CRAN or the development version from GitHub using pak.

    # From CRAN
    install.packages("chromote")
    
    # From GitHub (development version)
    # install.packages("pak")
    pak::pak("rstudio/chromote")
  2. Start a ChromoteSession and use the interactive viewer

    main

    To begin interacting with a headless browser, create a new ChromoteSession. You can use the $view() method to open an interactive viewer in your default web browser, which allows you to visually monitor what the headless browser is doing. This works best with Chromium-based browsers.

    library(chromote)
    
    b <- ChromoteSession$new()
    
    # Open a viewer for the headless browser
    b$view()
  3. Execute Chrome DevTools Protocol commands

    main

    Chromote provides full support for the Chrome DevTools Protocol. You can call any protocol method directly on the session object using the $Domain$method() syntax. For example, $Browser$getVersion() queries the browser for its version information.

    b <- ChromoteSession$new()
    b$Browser$getVersion()
  4. Take screenshots with $screenshot()

    main

    Chromote provides a $screenshot() convenience method to capture images of the browser content.

    • To save a screenshot of the entire page to a file named screenshot.png, call b$screenshot().
    • To capture a screenshot of specific elements, use the selector argument with a CSS selector.
    # Saves the full page to screenshot.png
    b$screenshot()
    
    # Takes a screenshot of elements picked out by CSS selector
    b$screenshot("sidebar.png", selector = ".sidebar")
  5. Navigate to a URL with $go_to()

    main

    The ChromoteSession object includes convenience methods for common tasks. Use $go_to(url) to instruct the browser to navigate to a specific web address.

    b$go_to("https://www.r-project.org/")