在RSelenium中指定下载文件夹

use*_*899 5 google-chrome r rselenium

RSelenium用来导航到一个网页,其中包含一个下载文件的按钮.我使用RSelenium单击此按钮下载文件.但是,文件默认下载到我的文件夹'downloads'中,而我想将文件下载到我的工作目录中.我尝试指定一个chrome配置文件,如下所示,但这似乎没有做到这一点:

wd <- getwd()
cprof <- getChromeProfile(wd, "Profile 1")
remDr <- remoteDriver(browserName= "chrome", extraCapabilities = cprof) 
Run Code Online (Sandbox Code Playgroud)

该文件仍然下载在"downloads"文件夹中,而不是我的工作目录中.怎么解决这个问题?

jdh*_*son 7

该解决方案涉及设置https://sites.google.com/a/chromium.org/chromedriver/capabilities中列出的相应chromeOptions .这是一个关于Windows 10盒子的例子:

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"
Run Code Online (Sandbox Code Playgroud)

  • 这将在启动时更改下载目录。chrome开始使用代码后,有没有办法更改它?@jdharrison (2认同)

Fre*_*olt 4

我一直在尝试替代方案,似乎@Bharath 的第一个评论是关于放弃摆弄首选项(似乎不可能做到这一点),而是从默认下载文件夹中移动文件到所需的文件夹是要走的路。使其成为便携式解决方案的技巧是找到默认下载目录在哪里\xe2\x80\x94当然它因操作系统而异你可以像这样得到)\xe2\x80\x94并且你需要找到用户的用户名也是:

\n\n
desired_dir <- "~/Desktop/cool_downloads" \nfile_name <- "whatever_I_downloaded.zip"\n\n# build path to chrome\'s default download directory\nif (Sys.info()[["sysname"]]=="Linux") {\n    default_dir <- file.path("home", Sys.info()[["user"]], "Downloads")\n} else {\n    default_dir <- file.path("", "Users", Sys.info()[["user"]], "Downloads")\n}\n\n# move the file to the desired directory\nfile.rename(file.path(default_dir, file_name), file.path(desired_dir, file_name))\n
Run Code Online (Sandbox Code Playgroud)\n