Hendrik
Hendrik

Reputation: 35

RSelenium - get text from result form

I have consulted SO thoroughly to avoid duplicates, but still seem not to be getting anywhere. Proudly RSelenium and docker both work on my mac. The problem is how to extract the kms result from: https://www.freemaptools.com/how-far-is-it-between.htm The result I am expecting is 960.467 kms. I have another 800 distances to check every year, so solving this problem is definitely worth the effort. I am getting only an empty line, please see [1] "" towards the end.

Does this happen because the kms result is a "read only" input field? How do I extract the km result? Thanks in advance.

## 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()

Upvotes: 0

Views: 731

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146620

The problem is you are trying to get text from an input element which has none. So the result is correct. you need get value

distance$getElementAttribute("value")

Also make sure you have enough delay for the values to get fetched and populated because there is a AJAX call in the background

Upvotes: 2

Related Questions