2016-01-29 21 views
3

我想從此National River Flow Archive(UK)網站獲取年度最大流量數據: http://nrfa.ceh.ac.uk/data/station/info/69032 使用RSelenium。 我無法找到協商下拉菜單的方法。目前使用我可以半自動化的過程:使用RSelenium從網頁表中檢索數據

library(RSelenium) 
checkForServer() 
startServer() 
remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4444, browserName = "firefox", platform = "LINUX") 
remDr$open() 
i <- "69032" 
remDr$navigate(paste0("http://nrfa.ceh.ac.uk/data/station/peakflow/", i)) 
# read the raw html and parse 
doc<-htmlParse(remDr$getPageSource()[[1]]) 
peak.flows <- as.numeric(readHTMLTable(doc)$tablesorter[, "Flow (m3/s)"]) 

這是一個黑客位,並涉及到了我要點擊頁面上的幾個按鈕,而不是讓RSelenium做到這一點。有關RSelenium如何從下拉菜單中選擇「峯值流量數據」選項卡,然後選擇「最大年度(AMAX)數據」選項的任何建議?

回答

2
library(RSelenium) 
checkForServer() 
startServer() 
remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4444, browserName = "firefox", platform = "LINUX") 
remDr$open() i <- "69032" 
remDr$navigate(paste0("http://nrfa.ceh.ac.uk/data/station/peakflow/", i)) 
remDr$findElement(using="css selector",'.selected a')$clickElement() 
Sys.sleep(5) 
remDr$findElement(using = "css selector", "#selectDataType")$clickElement() 
remDr$findElement(using = "css selector", "#selectDataType")$sendKeysToElement(list(key="down_arrow", key="enter")) 
Sys.sleep(2)` 

如果您想了解感興趣的元素的CSS ID,請將[SELECTOR GADGET]插件安裝到Chrome中。突出顯示要讓RSelenium單擊的元素,然後抓取CSS ID。