Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RSelenium与Javascript_Javascript_R_Selenium_Web Scraping - Fatal编程技术网

RSelenium与Javascript

RSelenium与Javascript,javascript,r,selenium,web-scraping,Javascript,R,Selenium,Web Scraping,我相当精通R,但对javaScript和其他语言一无所知。我想访问有关此公共可用数据集()的信息。特别是,我在一个数据框中列出了数千个形式为(“A1A1”)的邮政编码。我想将这些邮政编码提交到本网站,然后提取返回的选区名称。RSelenium看起来很理想,但我不知道如何让javascript工作。 我正在使用MacOS10.9.5,R3.0.3和RSelenium_1.3。Firefox是v。33,硒含量为2.44。下面的脚本有效 require(RSelenium) checkForServe

我相当精通R,但对javaScript和其他语言一无所知。我想访问有关此公共可用数据集()的信息。特别是,我在一个数据框中列出了数千个形式为(“A1A1”)的邮政编码。我想将这些邮政编码提交到本网站,然后提取返回的选区名称。RSelenium看起来很理想,但我不知道如何让javascript工作。 我正在使用MacOS10.9.5,R3.0.3和RSelenium_1.3。Firefox是v。33,硒含量为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')
require(RSelenium)
checkForServer()
startServer()

remDr您不需要在此处使用
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
将脚本作为参数和列表。如果列表中的任何元素属于类
webElement
然后它们可以像DOM元素一样在脚本中引用。在本例中,第一个元素(JavaScript中的零索引)是
webElement
,我们要求在JavaScript中单击它

此外,如果您检查按钮后面的源代码,您会发现当按下按钮时,它会调用
document.pcode.submit()
,因此在本例中,如果您想使用
executeScript
,您可以执行以下操作:

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