简体   繁体   中英

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.

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)

downloadPath <- paste0(getwd(), "/temp/") %>% stringr::str_replace_all("/", "\\\\\\\\") 
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

Code later downloads a file with clickElement()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM