2017-02-23 75 views
1

我試過所有的方法來自動化下拉選擇Rselenium?特別是,我可以使用findElement選擇下拉框,但是如何選擇一個選項呢?RSelenium-訪問下拉元素時出現網絡劃痕的錯誤

http://localizador.extra.com.br/storeSearch/?organization=EXTRA

我想湊這個網站。

我試過的代碼。

for(i in 2:15){ 
#This part of code is not able to change the option in dropdown 
webElem1a <- remDr$findElements(using='xpath',paste("//[@id='state']/option[",i,"]")) 
webElem1a$clickElement() 
loadmorebuttons<- remDr$findElement(using = 'css selector',".hidde") 
loadmorebuttons$clickElement() 

Sys.sleep(5) 


#Below this code works properly 
#Check the total Number of stores Available 
    webElem1a<-remDr$findElements(using = 'css selector',".storesFound") 
    Total_stores<-unlist(lapply(webElem1a,function(x){x$getElementText()})) 
    Total_stores<-as.numeric(Total_stores) 

#Load More view 
    if(Total_stores >= 7){ 
    for(j in 1:58){ 
     abc<-remDr$findElement(using = 'css selector',".moreResults") 
     abc$clickElement() 
    } 
    }else{ 
    print("") 
    } 

回答

1

可以使用selectTag方法:

library(RSelenium) 

rD <- rsDriver(verbose = F) 
remDr <- rD$client 
remDr$navigate("http://localizador.extra.com.br/storeSearch/?organization=EXTRA") 
selElem <- remDr$findElement("id", "state") 
opts <- selElem$selectTag() 
opts$elements[[2]]$clickElement() 

rm(rD) 
gc() 
+0

非常感謝@jdharrison。它解決了我的問題。 – Shubham

+0

使用'opts $ elements [[2]] $ clickElement()'以錯誤結束'錯誤:嘗試應用非函數'。我該如何解決這個問題? –