2017-09-03 25 views
0

我已经彻底咨询过,以避免重复,但似乎没有得到任何地方。自豪地RSelenium和码头工都在我的Mac上工作。问题是如何从中提取kms结果:https://www.freemaptools.com/how-far-is-it-between.htm我期待的结果是960.467 kms。每年我还有800个距离检查,所以解决这个问题绝对值得。我只得到一个空行,请参见[1]“”到最后。RSelenium - 从结果表格中获取文本

发生这种情况是因为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

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

distance$getElementAttribute("value") 

另外,还要确保你有足够的延迟值来获得牵强,人口,因为在后台一个AJAX调用

+0

非常感谢。有效。 – Hendrik

+0

太好了。你能接受它作为答案吗? –