mks212 mks212 - 1 month ago 12
R Question

Log In To Website Using RSelenium & phantomjs in R, Multiple Instances Of Class exist

I am trying to log in to this page: https://www.optionslam.com/accounts/login/ using the code on this post as a starting point, Scrape password-protected website in R

I have been able to populate the login fields, but cannot click on the log in button. If you look at the source of the page, the class of login is "red-button"

<input type="submit" value="Log in" class="red-button"/>


However, there is another form at the top of the page that also uses the same class, and the clickElement() command is clicking on it. Reading the RSelenium documentation, I can't find a way to either search for the 2nd instance of this class or look it up based on type="submit" or value="Log In".

Here is my code:

library(RSelenium)

pJS <- phantom() # start phantomjs

appURL <- 'https://www.optionslam.com/accounts/login/'
remDr <- remoteDriver(browserName = "phantomjs")
remDr$open()
remDr$navigate(appURL)
remDr$findElement("id", "id_username")$sendKeysToElement(list("user"))
remDr$findElement("id", "id_password")$sendKeysToElement(list("pass"))
remDr$findElement("class name", "red-button")$clickElement()


Thank you for your help.

Answer

Two options:

Use findElements to get both buttons and click on the 2nd one:

remDr$findElements("class name", "red-button")[[2]]$clickElement()

or use another selector method as @SymbolixAU suggests and target the 2nd element directly:

webElem <- remDr$findElement("css", ".red-button[value='Log in']")
webElem$getElementAttribute("outerHTML")

#[[1]]
#[1] "<input type=\"submit\" value=\"Log in\" class=\"red-button\">"

webElem$clearElement()