RSelenium 和 Javascript [英] RSelenium and Javascript

查看:24
本文介绍了RSelenium 和 Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 R 相当精通,但对 javaScript 和其他语言完全一无所知.我想访问有关此公开数据集的信息(http://fyed.elections.on.ca/fyed/en/form_page_en.jsp).特别是,我在数据框中有一个包含数千个 ('A1A1A1') 形式的邮政编码列表.我想将这些邮政编码中的每一个提交到此网站,然后提取返回的选区名称.RSelenium 似乎很理想,但我不知道如何让 javascript 工作.我正在使用带有 R 3.0.3 和 RSelenium_1.3 的 Mac OS 10.9.5.Firefox 是 v. 33,Selenium 是 2.44.以下脚本有效.

I'm fairly proficient in R, but completely ignorant regarding javaScript and other languages. I would like to like to access information on this publicly-available data set (http://fyed.elections.on.ca/fyed/en/form_page_en.jsp). In particular, I have a list of several thousand postal codes of the form ('A1A1A1') in a data frame. I would like to submit each of these postal codes to this website and then extract the name of the electoral district that is returned. RSelenium seems ideal, but I cannot figure out how to get the javascript to work. I am working on a Mac OS 10.9.5, with R 3.0.3 and RSelenium_1.3. Firefox is v. 33 and Selenium is 2.44. The following script works.

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 的语法.我很感激任何帮助.

I feel like I just don't understand the javascript commands necessary or the syntax of executeScript to make this work. I'd appreciate any help.

推荐答案

你不需要在这里使用 executeScript:

You dont need to use executeScript here:

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 代替,您可以将最后一行替换为:

if you wanted to use executeScript instead you would replace the last line with:

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

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

executeScript takes a script as an argument and a list. If any elements of the list are of class webElement then they can be referred to in the script like a DOM element. In this case the first element (zero index in JavaScript) is a webElement and we ask to click it in our JavaScript.

此外,如果你检查按钮背后的源代码,你会发现当它被按下时它会调用 document.pcode.submit() 所以在这种情况下,如果你想使用 更简单executeScript 你可以这样做:

Furthermore if you examine the source code behind the button you will find when it is pressed it calls document.pcode.submit() so more simply in this case if you wanted to use executeScript you could do:

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

这篇关于RSelenium 和 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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