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:
- crrri by Romain Lesur and Christophe Dervieux
- decapitated by Bob Rudis
- chradle by Miles McBain
The interface to Chromote is similar to chrome-remote-interface for node.js.
-
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 about using and programming with chromote:
- Get started
- Commands and events
- Synchronous vs. asynchronous usage
- Choosing which Chrome-based browser to use
Install the released version of chromote from CRAN:
install.packages("chromote")
Or install the development version from GitHub with:
# install.packages("pak")
pak::pak("rstudio/chromote")
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:
b$Browser$getVersion()
#> $protocolVersion
#> [1] "1.3"
#>
#> $product
#> [1] "HeadlessChrome/98.0.4758.102"
#>
#> $revision
#> [1] "@273bf7ac8c909cde36982d27f66f3c70846a3718"
#>
#> $userAgent
#> [1] "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/98.0.4758.102 Safari/537.36"
#>
#> $jsVersion
#> [1] "9.8.177.11"
If you have the viewer open and run the following, you’ll see the web page load in the viewer1:
b$Page$navigate("https://www.r-project.org/")
In the official Chrome DevTools Protocol (CDP) documentation, this is
the
Page.navigate
command.
In addition to full support of the CDP, ChromoteSession
objects also
some convenience methods, like $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")
Footnotes
-
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. ↩