Learn R Programming

selenium (version 0.1.4)

chrome_options: Custom browser options

Description

Create browser options to pass into the capabilities argument of SeleniumSession$new().

Usage

chrome_options(
  binary = NULL,
  args = NULL,
  extensions = NULL,
  prefs = NULL,
  ...
)

firefox_options(binary = NULL, args = NULL, profile = NULL, prefs = NULL, ...)

edge_options(binary = NULL, args = NULL, extensions = NULL, prefs = NULL, ...)

Value

A list of browser options, with Chrome options under the name goog:chromeOptions, Firefox options under moz:firefoxOptions, and Edge options under ms:edgeOptions.

Arguments

binary

Path to the browser binary.

args

A character vector of additional arguments to pass to the browser.

extensions

A character vector of paths to browser extension (.crx) files. These will be base64 encoded before being passed to the browser. If you have already encoded the extensions, you can pass them using I(). For Firefox, use a profile to load extensions.

prefs

A named list of preferences to set in the browser.

...

Additional options to pass to the browser.

profile

Path to a Firefox profile directory. This will be base64 encoded before being passed to the browser.

Details

These functions allow you to more easily translate between Selenium code in other languages (e.g. Java/Python) to R. For example, consider the following Java code, adapted from the the Selenium documentation

ChromeOptions options = new ChromeOptions();

options.setBinary("/path/to/chrome"); options.addArguments("--headless", "--disable-gpu"); options.addExtensions("/path/to/extension.crx"); options.setExperimentalOption("excludeSwitches", List.of("disable-popup-blocking"));

This can be translated to R as follows:

chrome_options(
  binary = "/path/to/chrome",
  args = c("--headless", "--disable-gpu"),
  extensions = "/path/to/extension.crx",
  excludeSwitches = list("disable-popup-blocking")
)

You can combine these options with non-browser specific options simply using c().

Note that Microsoft Edge options are very similar to Chrome options, since it is based on Chromium.

See Also

Examples

Run this code
# Basic options objects
chrome_options(
  binary = "/path/to/chrome",
  args = c("--headless", "--disable-gpu"),
  detatch = TRUE, # An additional option described in the link above.
  prefs = list(
    "profile.default_content_setting_values.notifications" = 2
  )
)


firefox_options(binary = "/path/to/firefox")

edge_options(binary = "/path/to/edge")

# Setting the user agent
chrome_options(args = c("--user-agent=My User Agent"))

edge_options(args = c("--user-agent=My User Agent"))

firefox_options(prefs = list(
  "general.useragent.override" = "My User Agent"
))

# Using a proxy server

chrome_options(args = c("--proxy-server=HOST:PORT"))

edge_options(args = c("--proxy-server=HOST:PORT"))

PORT <- 1
firefox_options(prefs = list(
  "network.proxy.type" = 1,
  "network.proxy.socks" = "HOST",
  "network.proxy.socks_port" = PORT,
  "network.proxy.socks_remote_dns" = FALSE
))

# Combining with other options
browser_options <- chrome_options(binary = "/path/to/chrome")

c(browser_options, list(platformName = "Windows"))

Run the code above in your browser using DataLab