Download file from internet via R despite the popup

前端 未结 2 508
天涯浪人
天涯浪人 2020-12-18 08:05

Downloading a file from the internet using R is easy and has been addressed previously.

My question regards how to get past a popup message that seems to prevent my

2条回答
  •  半阙折子戏
    2020-12-18 08:47

    This can be done with Selenium see https://github.com/ropensci/RSelenium.

    require(wdman)
    require(RSelenium)
    
    
    selPort <- 4444L
    fprof <- makeFirefoxProfile(list(browser.download.dir = "C:\\temp"
                                     ,  browser.download.folderList = 2L
                                     , browser.download.manager.showWhenStarting = FALSE
                                     , browser.helperApps.neverAsk.saveToDisk = "application/zip"))
    selServ <- selenium(port = selPort)
    remDr <- remoteDriver(extraCapabilities = fprof, port = selPort)
    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()
    

提交回复
热议问题