就像我在堆栈溢出之前发布任何问题一样,我想我已经尝试了一切。这是一个关于如何使用javascript和xml的学习经验所以我猜我的问题就在那里。
我的问题是如何获得点击javascript链接的包裹号码链接的结果?我已经尝试获取链接的xpath并使用遵循我的直觉的$ click方法,但这不对,或者至少不适合我。
Firefox 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 :(得分:5)
您可以使用RSelenium
执行此操作。见http://johndharrison.github.io/RSelenium/。免责声明我是RSelenium软件包的作者。可以在RSelenium basics和RSelenium: Testing Shiny apps查看有关操作的基本内容
{{3}}
如果您不确定选择了哪个元素,可以使用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))
#