Chromote is an R implementation of the Chrome DevTools
Protocol. It works
with Chrome, Chromium, Opera, Vivaldi, and other browsers based on
Chromium. By default it uses Google Chrome
(which must already be installed on the system). To use a different
browser, see vignette("which-chrome").
Chromote is not the only R package that implements the Chrome DevTools
Protocol. Here are some others:
This will start a headless browser and open an interactive viewer for it
in a normal browser, so that you can see what the headless browser is
doing.
library(chromote)
b <- ChromoteSession$new()
# In a web browser, open a viewer for the headless browser. Works best with
# Chromium-based browsers.
b$view()
The browser can be given commands, as specified by the Chrome
DevTools Protocol.
For example, $Browser$getVersion() (which corresponds to the
Browser.getVersion
in the API docs) will query the browser for version information:
If you have the viewer open and run the following, you’ll see the web
page load in the viewer[^1]:
b$go_to("https://www.r-project.org/")
In addition to full support of the Chrome Devtools Protocol,
ChromoteSession objects also have some convenience methods, like
$go_to() and $screenshot(). (See the Examples section below for more
information about screenshots.)
# Saves to screenshot.png
b$screenshot()
# Takes a screenshot of elements picked out by CSS selector
b$screenshot("sidebar.png", selector = ".sidebar")
A screenshot of the sidebar of
r-rproject.org, circa 2023.
[^1]: This simple example works interactively, but if you’re using
chromote to programmatically take screenshots you’ll want to read
vignette("example-loading-page") for a consistent and reliable
approach.
chromote
Chromote is an R implementation of the Chrome DevTools Protocol. It works with Chrome, Chromium, Opera, Vivaldi, and other browsers based on Chromium. By default it uses Google Chrome (which must already be installed on the system). To use a different browser, see
vignette("which-chrome").Chromote is not the only R package that implements the Chrome DevTools Protocol. Here are some others:
The interface to Chromote is similar to chrome-remote-interface for node.js.
Features
Install and use specific versions of Chrome from the Chrome for Testing service.
Offers a synchronous API for ease of use and an asynchronous API for more sophisticated tasks.
Full support for the Chrome DevTools Protocol for any version of Chrome or any Chrome-based browser.
Includes convenience methods, like
$screenshot()and$set_viewport_size(), for common tasks.Automatically reconnects to previous sessions if the connection from R to Chrome is lost, for example when restarting from sleep state.
Powers many higher-level packages and functions, like
{shinytest2}andrvest::read_html_live().Learn More
Learn more about using and programming with chromote:
Installation
Install the released version of chromote from CRAN:
Or install the development version from GitHub with:
Basic usage
This will start a headless browser and open an interactive viewer for it in a normal browser, so that you can see what the headless browser is doing.
The browser can be given commands, as specified by the Chrome DevTools Protocol. For example,
$Browser$getVersion()(which corresponds to the Browser.getVersion in the API docs) will query the browser for version information:If you have the viewer open and run the following, you’ll see the web page load in the viewer[^1]:
In addition to full support of the Chrome Devtools Protocol,
ChromoteSessionobjects also have some convenience methods, like$go_to()and$screenshot(). (See the Examples section below for more information about screenshots.)[^1]: This simple example works interactively, but if you’re using chromote to programmatically take screenshots you’ll want to read
vignette("example-loading-page")for a consistent and reliable approach.