Search code examples
rselenium-webdriverrselenium

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


I tried all ways to automate the dropdown selection in Rselenium? In particular, I can select the dropdown box using findElement but how does one select an option with it?

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

I am trying to scrape this website.

The code which i tried.

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

Solution

  • You can use the selectTag method:

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