RSelenium - 从结果表单中获取文本

时间:2017-09-03 09:13:35

标签: r selenium rselenium

我已经彻底咨询了SO以避免重复,但似乎仍然没有得到任何地方。骄傲的RSelenium和docker都在我的Mac上工作。问题是如何从https://www.freemaptools.com/how-far-is-it-between.htm中提取kms结果我期望的结果是960.467公里。我每年要再检查800个距离,所以解决这个问题绝对是值得的。我只得到一个空行,请参阅[1]""走到尽头。

这是否会发生,因为kms结果是"只读"输入字段?如何提取km结果?提前谢谢。

## Start docker in Launchpad
## docker pull selenium/standalone-firefox
## docker run -d -p 4445:4444 selenium/standalone-firefox

library(RSelenium)

remDr <- remoteDriver(remoteServerAddr = "192.168.99.100", port = 4445L, browserName = "firefox")
remDr$open()

# Proof that docker and RSelenium both work  -----------------------------------

#[1] "Connecting to remote server"
# $`moz:profile`
# [1] "/tmp/rust_mozprofile.VrWrcVvdncmw"
# 
# $rotatable
# [1] FALSE
# 
# $specificationLevel
# [1] 0
# 
# $`moz:accessibilityChecks`
# [1] FALSE
# 
# $acceptInsecureCerts
# [1] FALSE
# 
# $browserVersion
# [1] "55.0.3"
# 
# $platformVersion
# [1] "4.4.83-boot2docker"
# 
# $`moz:processID`
# [1] 52
# 
# $timeouts
# $timeouts$implicit
# [1] 0
# 
# $timeouts$pageLoad
# [1] 300000
# 
# $timeouts$script
# [1] 30000
# 
# 
# $browserName
# [1] "firefox"
# 
# $pageLoadStrategy
# [1] "normal"
# 
# $platformName
# [1] "linux"
# 
# $id
# [1] "32ad02e8-e98c-4061-bfe7-e89c7515cb2f"

# Simulate browser session and fill out form -----------------------------------

remDr$navigate('https://www.freemaptools.com/how-far-is-it-between.htm')

from <- remDr$findElement(using = 'xpath', value = './/*[@id="pointa"]')
to <- remDr$findElement(using = 'xpath', value = './/*[@id="pointb"]')

from$sendKeysToElement(list('London, UK'))
to$sendKeysToElement(list('Milan, Italy'))

show <- remDr$findElement(using = 'xpath', value = './/*[@id="content"]/form/table/tbody/tr[2]/td[4]/p')
show$sendKeysToElement(list(key = 'enter'))

distance <- remDr$findElement(using = 'xpath', value = './/*[@id="distance"]')

distance$getElementText()[[1]]
# [1] ""

remDr$quit()
remDr$closeServer()

1 个答案:

答案 0 :(得分:1)

问题是你试图从没有输入元素的文本中获取文本。所以结果是正确的。你需要得到价值

distance$getElementAttribute("value")

还要确保有足够的延迟时间来获取和填充值,因为后台有一个AJAX调用