browser_session: A persistent headless browser session

View source: R/cdp.R

browser_sessionR Documentation

A persistent headless browser session

Description

Start a headless browser (Chromium/Chrome/Edge) once and reuse it to load pages and evaluate JavaScript on them via the Chrome DevTools Protocol, instead of starting a new browser for every page as browser_dom() does. This is much faster for running many checks against different pages.

Usage

browser_session(
  browser = NULL,
  args = browser_args(),
  size = NULL,
  timeout = 60
)

Arguments

browser

Path to the web browser. By default, the browser is found via xfun:::find_browser(). If it cannot be found, you may set the global option options(xfun.browser = ) or environment variable R_XFUN_BROWSER to the path of the browser, or simply pass the path to the browser argument.

args

Command-line arguments passed to the browser, by default xfun:::browser_args().

size

The window size, e.g., c(1280, 1024); the browser default if NULL.

timeout

The maximum number of seconds to wait for the browser and the page (when query or wait is used).

Value

A list of functions: open(input, query, wait) loads a page (see browser_print() for the arguments) and returns the session invisibly; eval(expr) evaluates JavaScript code on the current page and returns the value of its last statement as a string; dom() returns the HTML of the current page; close() closes the browser (it is also closed when the session is garbage-collected or R exits).

Examples


s = xfun::browser_session()
f = tempfile(fileext = ".html")
writeLines('<p id="a">hello</p>', f)
s$open(f)
s$eval('document.getElementById("a").textContent')
s$close()


xfun documentation built on Nov. 30, 2026, 9:06 a.m.

Related to browser_session in xfun...