[英]Specify download folder in RSelenium
我正在使用RSelenium
导航到包含下载文件按钮的网页。 我使用 RSelenium 单击此按钮下载文件。 但是,文件默认下载在我的文件夹“下载”中,而我想将文件下载到我的工作目录中。 我尝试指定如下 chrome 配置文件,但这似乎没有完成这项工作:
wd <- getwd()
cprof <- getChromeProfile(wd, "Profile 1")
remDr <- remoteDriver(browserName= "chrome", extraCapabilities = cprof)
该文件仍下载在文件夹“下载”中,而不是我的工作目录中。 如何解决这个问题?
该解决方案涉及设置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"
我一直在尝试替代方案,似乎@Bharath 关于放弃摆弄首选项的第一个评论(似乎不可能这样做)而是将文件从默认下载文件夹移动到所需文件夹是要走的路。 使其成为可移植解决方案的技巧是找到默认下载目录的位置——当然它因操作系统而异(你可以这样获得)——而且你还需要找到用户的用户名:
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))
看看这个替代方式。 您的下载文件夹应该是空的。
down.list <- list.files(path = "E:/Downloads/",all.files = T,recursive = F)
file.rename(from = paste0("E:/Downloads/",down.list),to = paste0("E:/1/scrape/",down.list))
有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.