Wietze314
Wietze314

Reputation: 6020

RSelenium: Select option from dropdown

I use RSelenium to fill in a webform. To select an option from a dropdown I use the following:

xpathoption <- paste0("//select[@id = '",samplepatient[p,'name'],"']/option[",samplepatient[p,'value'],"]")

optionelem <- remDrv$findElement(using = "xpath", xpathoption)
selectelem <- remDrv$findElement(using = "xpath"
                   , paste0("//select[@id = '",samplepatient[p,'name'],"']"))
optionelem$clickElement()
selectelem$screenshot(display = T)

I use the following to check if the correct option was selected:

remDrv$findElement(using = "xpath", paste0("//select[@id = '",samplepatient[p,'name'],"']"))$getElementAttribute("value")[[1]]

The problem I have is when the clickElement() command is run twice, the result of the last command changes. I also checked the outcome with screenshot(). It also shows that a different option is switched to when using the clickElement() command twice.

Is there a different way to select the option from a dropdown list, that is not creating this behavior?

I use a docker on ubuntu with firefox 3.0.1.

The form is from a calculator I want to use. To open the form itself you need to first check the disclaimer, like so:

remDrv$navigate('http://riskcalculator.facs.org/RiskCalculator/')

remDrv$findElement(using = "xpath", "//input[@id = 'chkDisclaimer']")$clickElement()
Sys.sleep(1) 
remDrv$findElement(using = "xpath", "//input[@id = 'btnContinue']")$clickElement()
Sys.sleep(1)

a reproducable example after the disclaimer is:

#select age group
optionelem <- remDrv$findElement(using = "xpath", "//select[@id = 'AgeGroup']/option[3]")
selectelem <- remDrv$findElement(using = "xpath", "//select[@id = 'AgeGroup']")
#first attempt
optionelem$clickElement()
selectelem$getElementAttribute("value")
# result = 3
#second attempt
optionelem$clickElement()
selectelem$getElementAttribute("value")
# result = 1

Upvotes: 4

Views: 3623

Answers (2)

Wietze314
Wietze314

Reputation: 6020

As mentioned in one of the comments, the issue has not to do with RSelenium but with the docker used. I now use a chrome docker (standalone-chrome) that does not have the same issue with selecting an option in a dropdown.

Upvotes: 4

jdharrison
jdharrison

Reputation: 30465

I don't come across any issues when selecting options using clickElement for example:

remDrv$navigate('http://riskcalculator.facs.org/RiskCalculator/')
remDrv$findElement("id", "chkDisclaimer")$clickElement()
Sys.sleep(1) 
remDrv$findElement("id", "btnContinue")$clickElement()
Sys.sleep(1)
#select age group
ageElems <- remDrv$findElements("css", "#AgeGroup option")
ageElems[[3]]$clickElement()
#select Diabetes
diaElems <- remDrv$findElements("css", "#Diabetes option")
diaElems[[2]]$clickElement()
# Select Gender
genderElems <- remDrv$findElements("css", "#Gender option")
genderElems[[1]]$clickElement()

When running in Docker you can use "debug" image and a VNC viewer to see exactly whats happening in the browser.

Upvotes: 1

Related Questions