4

我想通过 Firefox 浏览器使用 RSelenium 从网站下载文件。我做的一切都正确(导航、选择正确的元素并写下我想要的);现在我单击“下载”按钮,然后会打开一个 firefox 弹出窗口并询问我是否要下载文件或“打开方式...”其他内容。

不幸的是,由于隐私限制,我无法编写示例。

我的问题是:如何切换到弹出窗口/警报并在需要时单击“确定”?

我尝试了以下方法均未成功:

remDrv$acceptAlert()     -> tells me: NoAlertOpenError  
remDrv$executeScript("driver.switchTo().alert().accept()")

我也试过方法

remDrv$getWindowHandles()

但即使弹出窗口打开,该命令也只返回一个窗口(开始窗口,而不是弹出窗口),因此我无法使用:

remDrv$switchToWindow()

切换到弹出窗口。

有任何想法吗?谢谢

4

1 回答 1

12

What you are seeing is not a popup it is a download dialog. The download dialog is native in all browsers and cannot be controlled with JavaScript. You can configure Firefox to automatically download for certain file types. You havent given us alot of information. It can be done by setting an appropriate profile. Here is an example that downloads some financial data. We set four options in a bespoke profile. We have to jump through some hoops selecting options before we get a file to download:

require(RSelenium)
fprof <- makeFirefoxProfile(list(browser.download.dir = "C:\\temp"
                                 ,  browser.download.folderList = 2L
                                 , browser.download.manager.showWhenStarting = FALSE
                                 , browser.helperApps.neverAsk.saveToDisk = "application/zip"))
RSelenium::startServer()
remDr <- remoteDriver(extraCapabilities = fprof)
remDr$open(silent = TRUE)
remDr$navigate("https://www.chicagofed.org/applications/bhc_data/bhcdata_index.cfm")
# click year 2012
webElem <- remDr$findElement("name", "SelectedYear")
webElems <- webElem$findChildElements("css selector", "option")
webElems[[which(sapply(webElems, function(x){x$getElementText()}) == "2012" )]]$clickElement()

# click required quarter

webElem <- remDr$findElement("name", "SelectedQuarter")
Sys.sleep(1)
webElems <- webElem$findChildElements("css selector", "option")
webElems[[which(sapply(webElems, function(x){x$getElementText()}) == "4th Quarter" )]]$clickElement()

# click button

webElem <- remDr$findElement("id", "downloadDataFile")
webElem$clickElement()
于 2015-04-20T22:49:35.707 回答