11

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?

jdharrison
  • 30,085
  • 4
  • 77
  • 89
user3387899
  • 601
  • 5
  • 18
  • 2
    Possible try to browse for methods to move a file from download folder and to the desired folder. – Bharath Feb 19 '16 at 14:47
  • This is already solved in the below link [http://stackoverflow.com/questions/25251583/downloading-file-to-specified-location-with-selenium-and-python] – Bharath Feb 19 '16 at 21:51
  • 1
    That question is about Selenium in Python. My question is about RSelenium as a package in R. I understand that the argument '"browser.download.dir" can fix the problem, but the documentation related to RSelenium does not seem to support this arument... – user3387899 Feb 22 '16 at 07:55
  • I know that for firefox R supports the "browser.download.dir", but this does not seem to be the case for chrome. – user3387899 Feb 22 '16 at 08:37
  • [http://stackoverflow.com/questions/34476422/downloading-a-pdf-using-rselenium] visit this link to get an idea as how you can connect this answer to [this] http://stackoverflow.com/questions/25251583/downloading-file-to-specified-location-with-selenium-and-python]. Type about:config in address bar of the firefox to change the profile of the firefox browser and achieve your answer – Bharath Feb 22 '16 at 20:08
  • Answered here https://stackoverflow.com/a/65201209/12135618 – Nad Pat Sep 17 '21 at 15:44

3 Answers3

12

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"
jdharrison
  • 30,085
  • 4
  • 77
  • 89
  • 2
    This changes the download directory at start up; is there is a way to change it after chrome has started using code? @jdharrison – xiaodai Jul 16 '17 at 10:08
  • I had to change the file paths to '\\' for the `download.default_directory` options and then everything worked for me. – Mxblsdl Jul 20 '22 at 21:40
4

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))
Community
  • 1
  • 1
Frederick Solt
  • 356
  • 3
  • 10
-2

Look this alternative way. Your download folder should be empty.

# List the files inside the folder
down.list <- list.files(path = "E:/Downloads/",all.files = T,recursive = F)

# Move all files to specific folder
file.rename(from = paste0("E:/Downloads/",down.list),to = paste0("E:/1/scrape/",down.list))
user438383
  • 5,716
  • 8
  • 28
  • 43