首页 > 解决方案 > RSelenium 找到并点击链接

问题描述

我下面的代码向日本地理定位网站发送请求,并获得一个新页面,但我无法让 RSelenium 定位并单击该链接。

library(XML)
library(RSelenium)

remDr <- remoteDriver(
  remoteServerAddr = "localhost",
  port = 4445L,
  browserName = "firefox"
)

remDr$open()
remDr$navigate("https://maps.gsi.go.jp")
remDr$screenshot(display = TRUE)
remDr$getCurrentUrl()

webElem <- remDr$findElement(using = "id", value = "query")
webElem$getElementAttribute("id")
webElem$highlightElement()
webElem$sendKeysToElement(list("茨城県行方郡玉造町","\uE007"))
remDr$screenshot(display = TRUE)

我想单击下面的节点并从新页面获取 URL,但我的尝试remDr$findElement都没有奏效。我错过了一些明显的东西吗?

<a href="javascript:void(0);" style="padding-left: 32px; background: url(&quot;image/mapicon/search_result.png&quot;) 0px 50% no-repeat;"><div class="title">茨城県行方郡</div><div class="muni">茨城県行方市</div></a>

标签: cssrxpathweb-scrapingrselenium

解决方案


像这样?

link <- remDr$findElement(using = "css selector", value = ".muni")
link$highlightElement()
link$clickElement()
remDr$getCurrentUrl()

推荐阅读