'Automate Headless Firefox in RSelenium with Profile

I would like to automate an instance of Firefox with RSelenium that is both headless and uses a custom profile (in the simple example below, one that does not load images). I can run Firefox headless (thanks to the discussion here) OR with the profile (source). However, I can't figure out how to combine them. It seems like it should be obvious but I'm striking out so far as I guess I just don't understand what extraCapabilities is expecting.

Example that gets either but not both to work below:

library(RSelenium)

port <- 4567L

# this runs headless firefox
driver <- rsDriver(browser = "firefox", check = FALSE, verbose = FALSE, port = port,
                        extraCapabilities = list("moz:firefoxOptions" = list(args = list('--headless'))))
remDr <- driver[["client"]]

# and this runs firefox with some profile
firefox_profile_me <- makeFirefoxProfile(list(permissions.default.image = 2L))
driver <- rsDriver(browser = "firefox", check = FALSE, verbose = FALSE, port = port,
                        extraCapabilities = firefox_profile_me)
remDr <- driver[["client"]]

Working with a profile isn't essential, as long as I can send options like "don't load images" to the Firefox driver along with the headless option.



Solution 1:[1]

A bit older question, but still, here's a working example of setting up a Firefox driver for auto downloading csv file in specific folder (without a docker):

library(RSelenium)
library(dplyr)

# downloading in folder temp
downloadPath <- file.path(getwd(), "temp") %>% stringr::str_replace_all("/", "\\\\\\\\") # dont question it
fprof <- makeFirefoxProfile(list(browser.download.dir = downloadPath,
                                 browser.download.folderList = 2L,
                                 browser.download.manager.showWhenStarting = FALSE,
                                 browser.helperApps.neverAsk.openFile = "text/csv",
                                 browser.helperApps.neverAsk.saveToDisk = "text/csv"))

# now add this to new list
exCap <- list(firefox_profile = fprof$firefox_profile, 
              "moz:firefoxOptions" = list(args = list('--headless')))

# and use it here
rD <- rsDriver(browser = "firefox", port = 4567L, extraCapabilities = exCap)

remDr <- rD$client

remDr$navigate("https://www.stats.govt.nz/large-datasets/csv-files-for-download/")
btn <- remDr$findElement(using = "xpath", 
                         value = "/html/body/div[12]/div/div/main/section/div/div/div/article/div/div[2]/article/ul/li[12]/div/div/h3/a")
btn$clickElement()

Note that you may need to wait a while for drivers to download for the first time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1