mks212 mks212 - 2 months ago 18
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: 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:


pJS <- phantom() # start phantomjs

appURL <- ''
remDr <- remoteDriver(browserName = "phantomjs")
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.


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']")

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