【问题标题】:Specify download folder in RSelenium在 RSelenium 中指定下载文件夹
【发布时间】:2016-05-31 23:48:56
【问题描述】:

我正在使用RSelenium 导航到包含下载文件按钮的网页。我使用 RSelenium 单击下载文件的此按钮。但是,这些文件默认下载在我的文件夹“下载”中,而我想将文件下载到我的工作目录中。我尝试如下指定 chrome 配置文件,但这似乎没有完成这项工作:

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

该文件仍下载在“下载”文件夹中,而不是我的工作目录中。怎么解决?

【问题讨论】:

  • 可能尝试浏览将文件从下载文件夹移动到所需文件夹的方法。
  • 这已经在下面的链接中解决了 [stackoverflow.com/questions/25251583/…
  • 那个问题是关于 Python 中的 Selenium。我的问题是关于 RSelenium 作为 R 中的一个包。我知道参数“browser.download.dir”可以解决问题,但是与 RSelenium 相关的文档似乎不支持这个参数...
  • 我知道firefox R支持“browser.download.dir”,但chrome似乎不是这样。
  • [stackoverflow.com/questions/34476422/… 访问此链接以了解如何将此答案连接到 [this] stackoverflow.com/questions/25251583/…。在firefox的地址栏中输入about:config来更改firefox浏览器的配置文件并实现你的答案

标签: r google-chrome rselenium


【解决方案1】:

解决方案涉及设置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"

【讨论】:

  • 这会在启动时更改下载目录; chrome开始使用代码后有没有办法改变它? @jdharison
【解决方案2】:

我一直在尝试替代方案,似乎@Bharath 的第一条评论是放弃摆弄首选项(似乎不可能这样做),而是将文件从默认下载文件夹移动到所需的文件夹是要走的路。使它成为可移植解决方案的诀窍是找到默认下载目录的位置——当然是it varies by os (which you can get like so)——你也需要to find the user's username

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))

【讨论】:

  • 这可以通过设置适当的 chromeOptions 来完成,参见示例
【解决方案3】:

看看这个替代方式。 您的下载文件夹应该是空的。

# 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 2022-06-23
    • 2020-12-29
    • 2023-03-12
    相关资源
    最近更新 更多