Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify download folder in RSelenium

I am using RSelenium to navigate towards a webpage which contains a button to download a file. I use RSelenium to click this button which downloads the file. However, the files are by default downloaded in my folder 'downloads', whereas I want to file to be downloaded in my working directory. I tried specifying a chrome profile as below but this did not seem to do the job:

wd <- getwd()
cprof <- getChromeProfile(wd, "Profile 1")
remDr <- remoteDriver(browserName= "chrome", extraCapabilities = cprof) 

The file is still downloaded in the folder 'downloads', rather than my working directory. How can this be solved?

like image 724
user3387899 Avatar asked Feb 19 '16 11:02

user3387899


2 Answers

The solution involves setting the appropriate chromeOptions outlined at https://sites.google.com/a/chromium.org/chromedriver/capabilities . Here is an example on a windows 10 box:

library(RSelenium)
eCaps <- list(
  chromeOptions = 
    list(prefs = list(
      "profile.default_content_settings.popups" = 0L,
      "download.prompt_for_download" = FALSE,
      "download.default_directory" = "C:/temp/chromeDL"
    )
    )
)
rD <- rsDriver(extraCapabilities = eCaps)
remDr <- rD$client
remDr$navigate("http://www.colorado.edu/conflict/peace/download/")
firstzip <- remDr$findElement("xpath", "//a[contains(@href, 'zip')]")
firstzip$clickElement()
> list.files("C:/temp/chromeDL")
[1] "peace.zip"
like image 75
jdharrison Avatar answered Nov 11 '22 08:11

jdharrison


I've been trying the alternatives, and it seems that @Bharath's first comment about giving up on fiddling with the prefs (it doesn't seem possible to do that) and instead moving the file from the default download folder to the desired folder is the way to go. The trick to making this a portable solution is finding where the default download directory is—of course it varies by os (which you can get like so)—and you need to find the user's username too:

desired_dir <- "~/Desktop/cool_downloads" 
file_name <- "whatever_I_downloaded.zip"

# build path to chrome's default download directory
if (Sys.info()[["sysname"]]=="Linux") {
    default_dir <- file.path("home", Sys.info()[["user"]], "Downloads")
} else {
    default_dir <- file.path("", "Users", Sys.info()[["user"]], "Downloads")
}

# move the file to the desired directory
file.rename(file.path(default_dir, file_name), file.path(desired_dir, file_name))
like image 3
Frederick Solt Avatar answered Nov 11 '22 07:11

Frederick Solt