简体   繁体   English

在 RSelenium 中指定下载文件夹

[英]Specify download folder in RSelenium

I am using RSelenium to navigate towards a webpage which contains a button to download a file.我正在使用RSelenium导航到包含下载文件按钮的网页。 I use RSelenium to click this button which downloads the file.我使用 RSelenium 单击此按钮下载文件。 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:我尝试指定如下 chrome 配置文件,但这似乎没有完成这项工作:

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?如何解决这个问题?

The solution involves setting the appropriate chromeOptions outlined at https://sites.google.com/a/chromium.org/chromedriver/capabilities .该解决方案涉及设置https://sites.google.com/a/chromium.org/chromedriver/capabilities 中概述的适当 chromeOptions。 Here is an example on a windows 10 box:这是 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"

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.我一直在尝试替代方案,似乎@Bharath 关于放弃摆弄首选项的第一个评论(似乎不可能这样做)而是将文件从默认下载文件夹移动到所需文件夹是要走的路。 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))

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) 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)) file.rename(from = paste0("E:/Downloads/",down.list),to = paste0("E:/1/scrape/",down.list))

It works!有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RSelenium 无头认证下载文件 - RSelenium headless authentication download file 尝试在 RSelenium 中下载缓存的图片 - Trying to download a cached picture in RSelenium RSelenium:直接导航到直接pdf下载 - RSelenium: hangs in navigate to direct pdf download RSelenium、Chrome、如何设置下载目录、文件下载错误 - RSelenium, Chrome, How to set download directory, file download error 我正在尝试为R v 3.5下载RSelenium,但它显示以下内容 - I'm trying to download RSelenium for R v 3.5 but it says the following 如何使用 RSelenium 从网页下载嵌入式 PDF 文件? - How to download embedded PDF files from webpage using RSelenium? RSelenium:设置用于Mac OS X的makeFirefoxProfile以下载文件而无需询问 - RSelenium: Setting makeFirefoxProfile for Mac OS X to download files without asking 使用 RSelenium 从 R 中的 GISAID 网站下载 Covid 患者元数据 - Download Covid patient metadata from GISAID website in R using RSelenium 远程 Linux 服务器上的 RSelenium 和 Docker - 无法下载文件 - RSelenium and Docker on remote Linux server - cannot download files 有没有办法在不使用 R 中的 RSelenium 的情况下从“网站按钮单击”下载 CSV 文件? - Is there a way to download a CSV file from "website button click" without using RSelenium in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM