browser_dom: Execute JavaScript on an HTML page via a headless browser and...

View source: R/browser.R

browser_domR Documentation

Execute JavaScript on an HTML page via a headless browser and dump the DOM

Description

Load an HTML page in a headless browser (Chromium/Chrome/Edge), execute all JavaScript on it, and return (or save) the rendered DOM as HTML.

Usage

browser_dom(
  input,
  output = NULL,
  fragment = FALSE,
  browser = NULL,
  query = NULL,
  wait = NULL,
  timeout = 60
)

Arguments

input

Path or URL to the HTML page.

output

An optional output file path. If not provided, the rendered HTML is returned as a character string.

fragment

Whether to return only the inner body content instead of the full HTML document.

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.

query

A named list (or vector) of query parameters to be appended to the URL of the page, e.g., list(paged = 1). If input is a file path, it will be converted to a ⁠file://⁠ URL first.

wait

A JavaScript expression that must be true before the page is printed (or its DOM is dumped), e.g., "window.ready". It is evaluated repeatedly until it returns a true value or the time is out. If it returns a Promise, the page is ready when the promise is resolved (regardless of its value). To wait for an event on window instead, use a named vector (or list) with the name of the event, and optionally a trigger event to be dispatched first to request something from the page, e.g., c(event = "ready") or c(event = "pagesjs:after", trigger = "pagesjs:paginate"). The page is always waited for until it is loaded. The command-line flags of the browser can only wait for a fixed time, so query and wait make the function control the browser via the Chrome DevTools Protocol instead, which is more reliable for pages that update themselves after they are loaded.

timeout

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

Value

If output is NULL, the rendered HTML as a character string; otherwise the output path (invisibly).

Examples


f = tempfile(fileext = ".html")
writeLines(c(
  '<html><body><div id="out"></div>',
  '<script>document.getElementById("out").textContent = "hello";</script>',
  '</body></html>'
), f)
xfun::browser_dom(f)


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

Related to browser_dom in xfun...