Rselenium - 无法在 iframe 中下载电子表格
Rselenium - cannot download spreadsheet in iframe
我正在尝试从以下网站下载土壤肥力数据:
https://soilhealth.dac.gov.in/publicreports/FertilityIndex
我已经到了可以在 iframe 中加载包含数据的电子表格的地步。但是我不知道如何访问下载按钮。这是我到目前为止的代码:
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()
}
}
}
非常感谢任何帮助!
填写表格后,我们可以下载 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()
我正在尝试从以下网站下载土壤肥力数据:
https://soilhealth.dac.gov.in/publicreports/FertilityIndex
我已经到了可以在 iframe 中加载包含数据的电子表格的地步。但是我不知道如何访问下载按钮。这是我到目前为止的代码:
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()
}
}
}
非常感谢任何帮助!
填写表格后,我们可以下载 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()