RSelenium-在网络抓取时访问下拉元素时出错

RSelenium-Error in accessing drop-down elements while web scraping

我尝试了所有方法来在 Rselenium 中自动化下拉菜单 selection?特别是,我可以 select 使用 findElement 的下拉框,但是如何 select 一个选项呢?

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("")
    }

您可以使用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()