使用RSelenium查找元素/元素

时间:2017-09-04 18:24:07

标签: r shiny rselenium

我正在尝试使用findElement

在此网站上找到一个单一元素

https://shiny.rstudio.com/gallery/datatables-options.html

例如“显示长度”。

稍后我想使用findElements查找所有元素。即,“显示长度”,......,“函数回调”。总共5个。

我尝试找到第一个元素(显示长度):

remDr$navigate("https://shiny.rstudio.com/gallery/datatables-options.html")
elems <- remDr$findElement("css selector", "#showcase-app-container > nav > div > ul > li.active > a") 
# Unable to locate element 

elems <- remDr$findElement("xpath", "//*[@id='showcase-app-container']/nav/div/ul/li[1]/a") 
# Unable to to locate element

我试图找到几个元素:

elems <- remDr$findElements("class", "nav navbar-nav") 
# Invalid or does not result in a WebElement

elems <- remDr$findElements("css selector", "#showcase-app-container > nav > div > ul") 
# list of 0

elems <- remDr$findElements("xpath", "//*[@id='showcase-app-container']/nav/div/ul") 
# list of 0

1 个答案:

答案 0 :(得分:5)

remDr$navigate("https://shiny.rstudio.com/gallery/datatables-options.html")
# htmlParse(remDr$getPageSource()[[1]]) to first the frames (only 1)

webElems <- remDr$findElements("css selector", "iframe")

remDr$switchToFrame(webElems[[1]])

elems <- remDr$findElements("css selector", "#showcase-app-container > nav > div > ul li")

unlist(lapply(elems, function(x) x$getElementText()))

# "Display length"    "Length menu"       "No pagination"     "No filtering"      "Function callback"