kennyB
kennyB

Reputation: 2083

RSelenium Radio Button click doesn't seem to be working

My example is:

library(RSelenium)
remDr <- remoteDriver()
remDr$open(silent = TRUE)
remDr$navigate("http://www.nngroup.com/articles/checkboxes-vs-radio-buttons/")
remDr$findElement("id", "three")$click()

Which doesn't seem to work. Can someone help with what's wrong?

Upvotes: 3

Views: 1695

Answers (2)

lazycipher
lazycipher

Reputation: 350

I don't know why but the things that I found over various forums didn't work for me.

  1. Using clickElement() method: which is explained by https://stackoverflow.com/a/26727185/5940139
webElem <- remDr$findElement("id", "three")
webElem$clickElement()
  1. Using sending enter key to element. https://github.com/ropensci/RSelenium/issues/20#issuecomment-499974957
webElem <- remDr$findElement("id", "three")
webElem$sendKeysToElement(list(key="enter"))
  1. Executing javascript code in browser.
remDr$executeScript("document.getElementById('three').click()")

The last one somehow worked for me when the first two didn't work for me.

Upvotes: 1

jdharrison
jdharrison

Reputation: 30425

You are on the right track. Looking at ?remoteDriver the click method is described as:

click(buttonId = 0) Click any mouse button (at the coordinates set by the last mouseMoveToLocation() command). buttonId - any one of 'LEFT'/0 'MIDDLE'/1 'RIGHT'/2. Defaults to 'LEFT'

This method is for clicking at a location on the screen. Your code can be changed slightly so that the findElement method result is assigned.

library(RSelenium)
# startServer() # start Selenium Server if needed
remDr <- remoteDriver()
remDr$open(silent = TRUE)
remDr$navigate("http://www.nngroup.com/articles/checkboxes-vs-radio-buttons/")
webElem <- remDr$findElement("id", "three")

> class(webElem)
[1] "webElement"
attr(,"package")
[1] "RSelenium"

Looking at the documentation for the webElement class there is a method clickElement:

webElem$clickElement()

Using this method should produce the required result.

Upvotes: 2

Related Questions