Skip to content
Advertisement

Rselenium – cannot download spreadsheet in iframe

I am trying to download soil fertility data from the following website:

https://soilhealth.dac.gov.in/publicreports/FertilityIndex

I have got to the point where I can load the spreadsheet containing the data within an iframe. However I cannot figure out how to access the download button. This is the code I have so far:

rD <- rsDriver(browser="firefox", port=4536L, verbose=F)
remDr <- rD[["client"]]

remDr$navigate("https://soilhealth.dac.gov.in/publicreports/FertilityIndex")

remDr$executeScript("document.getElementById('CycleId').value = 1;")

state_elem <- remDr$findElement(using = "id", value = "State_Code")
state_opts <- state_elem$selectTag()

state_script="document.getElementById('State_Code').onchange=GetDistrict(xx);
              document.getElementById('State_Code').value = xx;"
dist_script="document.getElementById('District_Code').onchange=GetSubdistrict(xx);
            document.getElementById('District_Code').value = xx;"
sub_dist_script="document.getElementById('sub_district_code').onchange=GetVillage(xx);
                document.getElementById('sub_district_code').value = xx;"

for (s in 2:length(state_opts$value)){
  remDr$executeScript(gsub("xx", state_opts$value[3], state_script, fixed = TRUE))


  dist_elem <- remDr$findElement(using = "id", value = "District_Code")

  dist_opts <- dist_elem$selectTag()
  
  for (d in 2:length(dist_opts$value)){
    remDr$executeScript(gsub("xx", dist_opts$value[2], dist_script, fixed = TRUE))
    
    sub_dist_elem <- remDr$findElement(using = "id", value = "sub_district_code")
    
    sub_dist_opts <- dist_elem$selectTag()
    
    for (j in 2:length(sub_dist_opts$value)){
      remDr$executeScript(gsub("xx", sub_dist_opts$value[2], sub_dist_script, fixed = TRUE))
      
      remDr$findElement(using = "css", value = "#confirmLink")$clickElement()
      Sys.sleep(5)
      
      table <- remDr$findElements(using = "css", "iframe")
      remDr$switchToFrame(table[[1]])
      
      remDr$findElement(using = "css", title="#CSV")$clickElement()
      
    }
  }
}

Any help would be much appriciated!

Advertisement

Answer

After you have filled the form we can download the data in csv format by first clicking on save button and then on csv option

# click save button 
remDr$findElement(using = "xpath",'//*[@id="ReportViewer1_ctl05_ctl04_ctl00_ButtonImgDown"]')$clickElement()

#click csv option 
remDr$findElement(using = "xpath",'//*[@id="ReportViewer1_ctl05_ctl04_ctl00_Menu"]/div[7]/a')$clickElement()
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement