sagarpabba / selenium-vba

Automatically exported from code.google.com/p/selenium-vba
0 stars 0 forks source link

Problems with reading API request result #68

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Operating system and version: Win 7
Office name and version : Office 2010
Browser name and version : Chrome, latest
SeleniumWrapper version : latest

Hi there. I'm, actually, new one in Selenium using, so I need you to guide me. 
Please )
I need to read the request  result to Google Distance Matrix Service. I've 
tried different ways like .GetText() or 
.findElementByName("distance").getAttribute("value"), and so on, but every time 
I have an automation error (run-time error '-2146233088 (80131500)') on line B 
= driver.findElementByName("distance").getAttribute("value").

I need to withdraw distances between cities. Any ideas?

My code:

Sub newOne()
Dim driver As New SeleniumWrapper.WebDriver
Dim a As String, B As String
a = 
"http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Se
attle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&senso
r=false"
driver.Start "chrome", a 
HideCommandPromptWindow = True
driver.Open a
B = driver.findElementByName("distance").getAttribute("value")
Cells(1, 1) = B
End Sub

Request result:
http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Sea
ttle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor
=false

Would be grateful for any advice.

Original issue reported on code.google.com by olena.kr...@gmail.com on 21 Apr 2014 at 12:22

GoogleCodeExporter commented 9 years ago
The description error is "no such element" which mean that the HTML node 
doesn't exist in the page.
If you look at the page source, you'll see that the data you are trying to 
extract is some JSON text, which is not handled by Selenium.
Selenium may not be the right tool here as there is no interaction to perform 
with the browser.
A better and faster way would be to use the WinHttpRequest object to download 
the page and the ScriptControl to parse the JSON data.

Anyway, this is how it can be done with Selenium using some JavaScript to parse 
the JSON text:

Dim driver As New SeleniumWrapper.WebDriver
driver.HideCommandPromptWindow = True
driver.Start "chrome", "http://maps.googleapis.com/maps/api/distancematrix"
driver.Open 
"/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=
bicycling&language=fr-FR&sensor=false"
distances = driver.executeScript("var 
rows=eval('('+document.getElementsByTagName('pre')[0].textContent+')' 
)['rows']; return [ rows[0]['elements'][0]['distance']['value'], 
rows[0]['elements'][1]['distance']['value'], 
rows[1]['elements'][0]['distance']['value'], 
rows[1]['elements'][1]['distance']['value']]")

The content of "distances" is an array with the 4 distances.

Original comment by florentbr on 22 Apr 2014 at 3:14

GoogleCodeExporter commented 9 years ago
Thank you for your help! It's work!

Original comment by olena.kr...@gmail.com on 23 Apr 2014 at 11:44

GoogleCodeExporter commented 9 years ago

Original comment by florentbr on 8 Sep 2014 at 5:40