2017-09-03 57 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

太好了。你能接受它作爲答案嗎? –