简体   繁体   English

Rselenium - 无法在 iframe 中下载电子表格

[英]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 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.我已经到了可以加载包含 iframe 中数据的电子表格的地步。但是我不知道如何访问下载按钮。 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!任何帮助将不胜感激!

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填写表格后,我们可以先点击保存按钮,然后点击 csv 选项,以 csv 格式下载数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM