Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rselenium - Popup

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

I also tried the method

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

to switch to the popup window.

Any ideas? Thanks

like image 893
GrilloRob Avatar asked Dec 15 '22 15:12

GrilloRob


1 Answers

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()
like image 187
jdharrison Avatar answered Dec 18 '22 12:12

jdharrison