2014-01-26 25 views
1

就像开始之前,我张贴在堆栈溢出的任何问题,我想我已经尝试了一切。这对我来说是一个学习经验,如何使用JavaScript和XML,所以我猜我的问题在那里。如何点击javascript“链接”?是我的xpath还是我的relenium/selenium使用?

我的问题是如何让点击是JavaScript链接包裹号链接的结果吗?我已经尝试获取链接的xpath,并使用跟随我的直觉的$ click方法,但这是不正确的,或者至少不适合我。

火狐26.0 [R 3.0.2

require(relenium) 
library(XML) 
library(stringr) 
initializing_parcel_number <- "00000000000" 
firefox <- firefoxClass$new() 
firefox$get("http://www.muni.org/pw/public.html") 
inputElement <- firefox$findElementByXPath("/html/body/form[2]/table/tbody/tr[2]/td/table[1]/tbody/tr[3]/td[4]/input[1]") 
inputElement$sendKeys(initializing_parcel_number) 
inputElement$sendKeys(key = "ENTER") 

##xpath to the first link. Or is it? 
first_link <- "/html/body/table/tbody/tr[2]/td/table[5]/tbody/tr[2]/td[1]/a" 

##How I'm trying to click the thing. 
linkElement <- firefox$findElementByXPath("/html/body/table/tbody/tr[2]/td/table[5]/tbody/tr[2]/td[1]/a") 
linkElement$click() 
+0

没有HTML,就没有答案。 –

+0

听起来有诗意。我应该向您提供迄今为止在代码中已连接到的url的html吗? – cylondude

+0

作为问题的一部分,您应该发布HTML,理想情况下只是一个相关范围的片段。 –

回答

4

可以使用RSelenium做到这一点。见http://johndharrison.github.io/RSelenium/。免责声明我是RSelenium软件包的作者。在操作的基本小品可以在RSelenium basicsRSelenium: Testing Shiny apps

如果您不能确定选择什么元素可以使用highlightElement实用方法在webElement类看到注释掉的代码来查看。 元素点击事件在这种情况下不会起作用。您需要使用javascript模拟点击:

require(RSelenium) 
# RSelenium::startServer # if needed 
initializing_parcel_number <- "00000000000" 
remDr <- remoteDriver() 
remDr$open() 
remDr$navigate("http://www.muni.org/pw/public.html") 
webElem <- remDr$findElement(using = "name", "PAR1") 
# webElem$highlightElement() # to visually check what elemnet is selected 
webElem$sendKeysToElement(list(initializing_parcel_number, key = "enter")) 
# get first link containing javascript:getParcel 
webElem <- remDr$findElement(using = "css selector", '[href*="javascript:getParcel"]') 
# webElem$highlightElement() # to visually check what elemnet is selected 
# send a webElement as an argument. 
remDr$executeScript("arguments[0].click();", list(webElem)) 

#  
+0

我无法连接到您的文档的RPubs,但不适用于其他人。 – cylondude

+0

这表明硒服务器没有运行。如果你已经安装了这个软件包,这个插件还应该位于/ docs目录下的软件包中。 Selenium服务器在RSelenium Basics小插件中解释。 – jdharrison

+1

哇。这真是太棒了。你有没有做过任何推广?我是世界上听到这个包裹的最后一个人。谢谢! – cylondude

相关问题