4

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

GrilloRob
  • 262
  • 1
  • 3
  • 15
  • First read [this](http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/). – reinierpost Apr 20 '15 at 22:24

1 Answers1

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()
jdharrison
  • 30,085
  • 4
  • 77
  • 89
  • Thanks for your reply, your example works. Unfortunately the extension of my download file is .DAT, and I tried browser.helperApps.neverAsk.saveToDisk = "DAT file, application/octet-stream, zz-application/zz-winassoc-dat, application/x-download, application/octet-binary" with no success. I forgot to mention that I am on Mac computer, may it affect something? – GrilloRob Apr 21 '15 at 05:47
  • You need to discover the mime type the server is sending for this file. – jdharrison Apr 21 '15 at 06:38
  • Hi @jdharrison, do you have any suggestion for me to find the mime type the server is sending? The file is .dat, and the suggested mime type would be "application/octet-stream", and I tried with no success. Is there any option available to say to firefox "download everything without asking" ? Thanks in advance – GrilloRob Apr 22 '15 at 05:34
  • Firefox does not have a download everything setting. To get the mime type if you are using Mac maybe try something like httpscoop or burpproxy. You can use something like HttpFox with firefox which is pretty straightforward. A ".dat" file could be anything and it really depends what the server is sending as a mime type. Using any traffic sniffer it should be straightforward to get the mime type unfortunately I cant help you any further without the particular url. – jdharrison Apr 22 '15 at 06:17
  • Is your ".dat" file plain text? You could try a "text/plain" mime type. – jdharrison Apr 22 '15 at 06:20
  • Thanks @jdharrison for your help, with HttpFox i have been able to understand the mime type, that in this case it is: application/x-unknown. Thanks again! – GrilloRob Apr 22 '15 at 21:36