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

痞子三分冷 提交于 2019-12-01 21:51:27

问题


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.


回答1:


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()


来源:https://stackoverflow.com/questions/40251904/log-in-to-website-using-rselenium-phantomjs-in-r-multiple-instances-of-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!