当前位置: 首页 > 面试题库 >

RSelenium和Javascript

佴阳曦
2023-03-14
问题内容

我对R相当精通,但是对javaScript和其他语言一无所知。我想访问有关此公开可用数据集的信息(http://fyed.elections.on.ca/fyed/zh-
CN/form_page_en.jsp
)。尤其是,我在数据框中列出了几千个(A1A1A1)形式的邮政编码。我想将这些邮政编码中的每一个都提交到此网站,然后提取返回的选举区的名称。RSelenium似乎很理想,但是我无法弄清楚如何使JavaScript正常工作。我正在使用R
3.0.3和RSelenium_1.3的Mac OS 10.9.5。Firefox是第33版,Selenium是2.44。以下脚本有效。

require(RSelenium)
checkForServer()
startServer()
remDr<-remoteDriver()
remDr$open()
remDr$getStatus()
remDr$navigate("http://fyed.elections.on.ca/fyed/en/form_page_en.jsp")

#After inspecting the source code, you can see the input box has the id 'pcode', for postal code
webElem<-remDr$findElement(using = 'id', value = "pcode")
webElem$getElementAttribute('id')

#This is where I am stuck
remDr$executeScript(script='arguments[0].click(m1p4v4)', list(webElem))

#Utlimately, I have a list of several thousand postal codes, so I would like to create a loop     through to extract all the district names that are stored on the pages that are returned with a successful javascript (see previous command). Three real postal codes that return results are as follows:  
p.codes<-c('m1p4v4', 'n3t2y3', 'n2h3v1')

我觉得我只是不了解使这项工作所需的javascript命令或executeScript的语法。我将不胜感激。


问题答案:

您不需要在executeScript这里使用:

require(RSelenium)
checkForServer()
startServer()
remDr<-remoteDriver()
remDr$open()
remDr$getStatus()
remDr$navigate("http://fyed.elections.on.ca/fyed/en/form_page_en.jsp")

p.codes<-c('m1p4v4', 'n3t2y3', 'n2h3v1')
webElem<-remDr$findElement(using = 'id', value = "pcode")
webElem$sendKeysToElement(list(p.codes[1])) # send the first post code to the element

remDr$findElement("id", "en_btn_arrow")$clickElement() # find the submit button and click it

如果您想使用它,executeScript则可以将最后一行替换为:

remDr$executeScript("arguments[0].click();"
                , list(remDr$findElement("id", "en_btn_arrow")))

executeScript将脚本作为参数和列表。如果列表中的任何元素属于类,
webElement则可以像DOM元素一样在脚本中引用它们。在这种情况下,第一个元素(JavaScript中的零索引)是a
webElement,我们要求在JavaScript中单击它。

此外,如果您检查按钮后面的源代码,就会发现在按下按钮时它会document.pcode.submit()更简单地调用(在这种情况下,如果您想使用),executeScript您可以执行以下操作:

remDr$executeScript("document.pcode.submit();")


 类似资料:
  • 问题内容: 我想使用Firefox浏览器,使用RSelenium从网站下载文件。我正确地完成了所有操作(导航,选择正确的元素并写下我想要的内容);现在,我单击“下载”按钮,然后打开一个Firefox弹出窗口,并询问我是否要下载文件或“用…打开”。 不幸的是,由于隐私限制,我无法编写示例。 我的问题是:如何在需要时切换到弹出窗口/警报并单击“确定”? 我尝试了以下方法,但均未成功: 我也试过了 但是

  • 我正试图抓取一个页面,获取一盘国际象棋的移动列表,该列表位于右侧菜单的“移动”选项卡下。 在浏览器中手动单击“移动”选项卡时,我可以通过 哪个(正确地)返回 当尝试通过单击按钮时,使用 似乎什么都没有发生,我不知道如何进行故障排除。 如何通过模拟单击(活动事件侦听器)切换到“移动”选项卡? 额外提示:是否可以使用rvest软件包?

  • 问题内容: 我正在基于RSelenium Basics CRAN页面 运行以下脚本: 这将产生以下错误: 基于GitHub上此对话的评论,我对命令进行了如下修改: 然后,我在控制台中收到以下错误: 然后弹出的Java提示中的错误: 我不确定缺少Opera驱动程序是实际错误还是警告。无论如何,我都想使用Chrome,所以好像没关系。我究竟做错了什么? 问题答案: 通过将来自许多不同来源的信息拼凑在一

  • 要等待dom加载,是否需要等待这个命令 给出结果以便dom就绪

  • 问题内容: 我正在使用Rselenium导航到网页。以下代码正在执行此操作。我没有提供该网址,因为我在需要VPN连接的公司中使用该网址: 导航到该网页后,在html源代码中,有下表: 现在的问题是如何提取该表的内容?请假定该URL不存在,否则我可以使用XML函数:readHTMLTable(remDr $ getCurrentUrl())。但这出于某些原因不起作用。我只需要使用remoteDriv

  • 我知道这个问题以前被问过多次,但没有一个解决方案对我有帮助。 3个月前,我成功地做到了这一点,但现在我陷入了困境,没有改变任何让我困惑的事情,我真的不知道该怎么办! 1:基本信息:win 7 Rselenium standalone 3.13.0(试用过3.9.1,但不起作用,三个月前就开始工作了)chrome版本v64。0.3282.140(32位)chromedriver 2.37应兼容。(还