使用RSelenium&登录到网站R中的phantomjs,存在类的多个实例 [英] Log In To Website Using RSelenium & phantomjs in R, Multiple Instances Of Class exist

查看:129
本文介绍了使用RSelenium&登录到网站R中的phantomjs,存在类的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试登录此页面: https://www.optionslam.com/account/login/,以本文中的代码为起点,

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"/>

但是,页面顶部还有另一种形式也使用相同的类,并且clickElement()命令正在单击它.阅读RSelenium文档,我找不到搜索此类的第二个实例或基于type ="submit"或value ="Log In"查找它的方法.

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".

这是我的代码:

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

谢谢您的帮助.

推荐答案

两个选项:

使用findElements同时获得两个按钮,然后单击第二个按钮:

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

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

或使用其他选择器方法(如@SymbolixAU建议)并直接定位第二个元素:

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

这篇关于使用RSelenium&amp;登录到网站R中的phantomjs,存在类的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆