Skip to contents

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, ...)

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.

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.

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.

Examples

# 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
  )
)
#> $`goog:chromeOptions`
#> $`goog:chromeOptions`$binary
#> [1] "/path/to/chrome"
#> 
#> $`goog:chromeOptions`$args
#> $`goog:chromeOptions`$args[[1]]
#> [1] "--headless"
#> 
#> $`goog:chromeOptions`$args[[2]]
#> [1] "--disable-gpu"
#> 
#> 
#> $`goog:chromeOptions`$prefs
#> $`goog:chromeOptions`$prefs$profile.default_content_setting_values.notifications
#> [1] 2
#> 
#> 
#> $`goog:chromeOptions`$detatch
#> [1] TRUE
#> 
#> 


firefox_options(binary = "/path/to/firefox")
#> $acceptInsecureCerts
#> [1] TRUE
#> 
#> $`moz:firefoxOptions`
#> $`moz:firefoxOptions`$binary
#> [1] "/path/to/firefox"
#> 
#> 
#> $`moz:debuggerAddress`
#> [1] TRUE
#> 

edge_options(binary = "/path/to/edge")
#> $`ms:edgeOptions`
#> $`ms:edgeOptions`$binary
#> [1] "/path/to/edge"
#> 
#> 

# Setting the user agent
chrome_options(args = c("--user-agent=My User Agent"))
#> $`goog:chromeOptions`
#> $`goog:chromeOptions`$args
#> $`goog:chromeOptions`$args[[1]]
#> [1] "--user-agent=My User Agent"
#> 
#> 
#> 

edge_options(args = c("--user-agent=My User Agent"))
#> $`ms:edgeOptions`
#> $`ms:edgeOptions`$args
#> $`ms:edgeOptions`$args[[1]]
#> [1] "--user-agent=My User Agent"
#> 
#> 
#> 

firefox_options(prefs = list(
  "general.useragent.override" = "My User Agent"
))
#> $acceptInsecureCerts
#> [1] TRUE
#> 
#> $`moz:firefoxOptions`
#> $`moz:firefoxOptions`$prefs
#> $`moz:firefoxOptions`$prefs$general.useragent.override
#> [1] "My User Agent"
#> 
#> 
#> 
#> $`moz:debuggerAddress`
#> [1] TRUE
#> 

# Using a proxy server

chrome_options(args = c("--proxy-server=HOST:PORT"))
#> $`goog:chromeOptions`
#> $`goog:chromeOptions`$args
#> $`goog:chromeOptions`$args[[1]]
#> [1] "--proxy-server=HOST:PORT"
#> 
#> 
#> 

edge_options(args = c("--proxy-server=HOST:PORT"))
#> $`ms:edgeOptions`
#> $`ms:edgeOptions`$args
#> $`ms:edgeOptions`$args[[1]]
#> [1] "--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
))
#> $acceptInsecureCerts
#> [1] TRUE
#> 
#> $`moz:firefoxOptions`
#> $`moz:firefoxOptions`$prefs
#> $`moz:firefoxOptions`$prefs$network.proxy.type
#> [1] 1
#> 
#> $`moz:firefoxOptions`$prefs$network.proxy.socks
#> [1] "HOST"
#> 
#> $`moz:firefoxOptions`$prefs$network.proxy.socks_port
#> [1] 1
#> 
#> $`moz:firefoxOptions`$prefs$network.proxy.socks_remote_dns
#> [1] FALSE
#> 
#> 
#> 
#> $`moz:debuggerAddress`
#> [1] TRUE
#> 

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

c(browser_options, list(platformName = "Windows"))
#> $`goog:chromeOptions`
#> $`goog:chromeOptions`$binary
#> [1] "/path/to/chrome"
#> 
#> 
#> $platformName
#> [1] "Windows"
#>