Reading values ​​in a drop-down menu using RSelenium

I use RSelenium to navigate sites and interact with elements.

Question: using RSelenium, how can I view the list of options in the drop-down menu so that I can identify the last month and use this to set the drop-down menu to the correct value?

In a specific site, a drop-down menu is provided to the user to set the month of the year, thus defining the endpoint of the date range used in turn to display or download monthly data. As additional months of data become available throughout the year, options are in the drop-down list of changes.

Dropdown Options

6 , . , 4-6 , . 1 12 - , , .

, , ( , ), "7", , . , , X Y.

, Selenium -, , RSelenium.

- , .

library(RSelenium)
RSelenium::checkForServer()
RSelenium::startServer()

remDr <- remoteDriver(remoteServerAddr = "localhost",
                      port = 4444,
                      browserName = "firefox"
                      )
remDr <- remoteDriver()
Sys.sleep(5)
remDr$open()
remDr$getStatus()

remDr$navigate("http://jamaserv.jama.or.jp/newdb/eng/index.html")

## Switch to left frame
frameElems <- remDr$findElements(using = "tag name", "frame")
sapply(frameElems, function(x){x$getElementAttribute("src")})
remDr$switchToFrame(frameElems[[1]])

webElem <- remDr$findElement(using = 'xpath', "//select[@name='txtTargetToMm']")

## Use javascript to set values
script <- paste0("arguments[0].value = '", 2, "'; arguments[0].onchange();") # set to February
remDr$executeScript(script, list(webElem))
webElem$getElementAttribute('value')    # check to see if it worked
+4
1

selectTag HTML select:

library(RSelenium)

rD <- rsDriver(verbose = F)
remDr <- rD$client
remDr$navigate("http://jamaserv.jama.or.jp/newdb/eng/index.html")

## Switch to left frame
frameElems <- remDr$findElements(using = "tag name", "frame")
sapply(frameElems, function(x){x$getElementAttribute("src")})
remDr$switchToFrame(frameElems[[1]])

webElem <- remDr$findElement(using = 'xpath', "//select[@name='txtTargetToMm']")
opts <- webElem$selectTag()

> opts$text
[1] "JANUARY"   "FEBRUARY"  "MARCH"     "APRIL"     "MAY"       "JUNE"      "JULY"      "AUGUST"   
[9] "SEPTEMBER" "OCTOBER"   "NOVEMBER"  "DECEMBER" 
> opts$value
[1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
> opts$selected
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE

# select the third month (march)
opts$elements[[3]]$clickElement()

# check whether it is selected
opts <- webElem$selectTag()
opts$selected

> opts$selected
[1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

rm(rD)
gc()
+5

Source: https://habr.com/ru/post/1599563/


All Articles