Rselenium-弹出窗口 [英] Rselenium - Popup

查看:210
本文介绍了Rselenium-弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Firefox浏览器使用RSelenium从网站上下载文件. 我正确地完成了所有操作(导航,选择正确的元素并写下我想要的内容); 现在,我单击下载"按钮,然后会打开一个firefox弹出窗口,询问我是否要下载文件或用...打开".

I want to download a file from a website using RSelenium, with Firefox browser. I do everything correctly (navigate, select the correct element and write what I want); now I click the "download" button, then a firefox popup opens and ask me if I want to download the file or "open with..." something else.

很遗憾,由于隐私限制,我无法编写示例.

Unfortunately I cannot write an example due to privacy constraints.

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

My question is: how can I switch to the popup window / alert and click "OK" when needed?

我尝试了以下方法,但没有成功:

I tried the following methods with no success:

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

我也尝试了方法

remDrv$getWindowHandles()

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

but even if the popup is open, the command return me one window only (the beginning one, not the popup), therefore I'm not able to use the:

remDrv$switchToWindow()

切换到弹出窗口.

有什么想法吗? 谢谢

推荐答案

您看到的不是弹出窗口,而是下载对话框.下载对话框在所有浏览器中都是本地的,不能用JavaScript进行控制.您可以将Firefox配置为针对某些文件类型自动下载.您没有给我们很多信息. 可以通过设置适当的配置文件来完成.这是一个下载一些财务数据的示例.我们在定制配置文件中设置了四个选项.在下载文件之前,我们必须跳过一些箍选择选项:

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

这篇关于Rselenium-弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆