Search code examples
rrselenium

get the values from drop down menu using Rselenium


I need to get data from a website but the code to get the values from a dropdown menu is not working, this code works fine for other websites but not for this one. I don't know why.

Below is the code and the website that I am trying to scrape. I'm RSelenium for this and I want to use this package for this tasks if possible.

library(tidyverse)
library(RSelenium)

rD <- try(rsDriver(browser = "chrome"
                       ,verbose = TRUE
                       ,chromever = "91.0.4472.101"))

remDr <- try(rD[["client"]])

remDr$navigate("https://assist.org/")

Sys.sleep(2)

DropDownOptions <- XML::htmlParse(remDr$getPageSource()[[1]]) %>%
    XML::xmlRoot(.) %>% 
    XML::getNodeSet('//select[@id="academicYear"]/option')
  
DropDownOptions <- data.frame(ID = sapply(DropDownOptions, XML::xmlGetAttr, "value")
                                   ,Name = sapply(DropDownOptions, XML::xmlValue))

DropDownOptions                        
data frame with 0 columns and 0 rows

```


Solution

  • So you want to select the academic year,

    library(RSelenium)
    driver = rsDriver(port = 4741L, browser = c("firefox"))
    remDr <- driver[["client"]]
    remDr$navigate("https://assist.org/")
    #Select the drop down menu on Academic Year 
    remDr$findElement(using = "xpath",'//*[@id="transfer-agreement-search"]/div[1]/ng-select/div') -> dropdownmenu
    dropdownmenu$clickElement()
    # Select the Academic year 2018-2019 (fullXpath)
    remDr$findElement(using = "xpath",'/html/body/app-root/div[2]/app-home-component/section[2]/app-form-container/div/div[1]/app-transfer-agreements-form/div/div[2]/form/div[1]/ng-select/ng-dropdown-panel/div/div[2]/div[4]') -> acdyear
    acdyear$clickElement()