使用 RSelenium 下载 Excel 个文件

Download Excel Files with RSelenium

我需要从数据库下载一个 excel 文件(我无法提供代码的原因)。我可以使用 RSelenium 单击下载图标。接下来发生的是通常的对话-window 打开询问我是否要保存文件或打开它。如何禁止显示此消息并将文件下载到文件夹中?

我发现了一个关于 pdf 的类似问题 。答案表明应该可以通过指定 extraCapabilities:

remDr <- remoteDriver(remoteServerAddr = "localhost", 
                  browserName = "firefox",
                  extraCapabilities = someCapabilities,
                  port = 4444)

不幸的是,我不知道如何正确设置 extraCapabilities

有人能给我指路吗?感谢您的帮助。

编辑

我知道 here 提供的解决方案,希望能够使用 extraCapabilities-方法。

这是一个例子:

library(RSelenium)
startServer()
remDr <- remoteDriver(extraCapabilities = makeFirefoxProfile(list(
  "browser.helperApps.neverAsk.saveToDisk"="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
))
remDr$open()
url <- "http://www.iwh-halle.de/e/fdz/IntBankLib/data/downloads/databases.xlsx"
remDr$navigate(url)
file.exists(file.path("~/Downloads/", basename(url)))
# [1] TRUE

请注意 content type 必须匹配:

library(httr)
HEAD(url)$headers$`content-type`
# [1] "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

尽管您应该可以使用像 * 这样的通配符。