r - 如何单击 javascript "link"?是我的 xpath 还是我的 relenium/selenium 用法?

标签 r selenium xpath

就像在我将问题发布到堆栈溢出之前的任何问题的开始一样,我想我已经尝试了一切。这对我来说是一次关于如何使用 javascript 和 xml 的学习经历,所以我猜我的问题就在那里。

我的问题是如何获得点击包裹编号链接的结果是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()

最佳答案

您可以使用 RSelenium 执行此操作。参见 http://johndharrison.github.io/RSelenium/ .免责声明 我是 RSelenium 包的作者。可以在 RSelenium basics 查看有关操作的基本插图。和 RSelenium: Testing Shiny apps

如果您不确定选择了哪个元素,可以使用 webElement 类中的 highlightElement 实用程序方法,请参阅注释掉的代码。 元素点击事件在这种情况下不起作用。您需要使用 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))

#    

关于r - 如何单击 javascript "link"?是我的 xpath 还是我的 relenium/selenium 用法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21369254/

相关文章:

r - 相同的查询,不同的结果。可能的原因?

r - 将常数乘以前一行并将结果累加到 R 中

json - 将 json 读入 R

python - 如何使用selenium python从动态表中提取数据?

Python Selenium StaleElementReferenceException : Message: stale element reference: element is not attached to the page document

r - 用R中的日期中位数插补数据

python - 升级到 Chromedriver 76 导致警报自动关闭

r - XPath 选择和连接所有文本节点

sql-server - 带有没有命名空间的 SQL Server 的 XPath

xml - 如何更新System.Xml.XmlDocument中的节点值?