-1

I was trying to scrape a website where you need to pass the address to get the coordinates of that address. I was able to pass the address and display the latitude and longitude in the browser but unable to retrieve it.

Here is how it goes.

Using this code I got the latitude and longitude displayed in the browser.

from selenium import webdriver
chrome_path = r"C:\Users\Himanshu Poddar\Desktop\chromedriver.exe"
url = 'https://www.latlong.net/convert-address-to-lat-long.html'
wd = webdriver.Chrome(chrome_path)
wd.get(url)

# Get the input element to which address is to be passed
inputElement = wd.find_element_by_xpath('//input[@placeholder="Type address here to get lat long"]')
# send the address
inputElement.send_keys('Domlur, Bangalore')
# click on get the coordinates button
wd.find_element_by_id('btnfind').click()

Now the lat and lon are displayed:

enter image description here

How do I get the latitude and longitude which are generated dynamically in the input field and therefore couldn't find in the inspect element.

The inspect element for latitude and longitude are

Latitude

<div class="col-6 m2">
<label for="lat">Latitude</label>
<input type="text" name="lat" id="lat" placeholder="lat coordinate">
</div>

Longitude

<div class="col-6 m2">
<label for="lng">Longitude</label>
<input type="text" name="lng" id="lng" placeholder="long coordinate">
</div>

which does not contain the displayed latitude and longitude.

EDIT : I am looking for the function that returns the latlong value, can we do it using the execute_script of selenium

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Himanshu Poddar
  • 7,112
  • 10
  • 47
  • 93
  • Possible duplicate of [Get value of an input box using Selenium (Python)](https://stackoverflow.com/questions/25580569/get-value-of-an-input-box-using-selenium-python) – JeffC Feb 01 '19 at 16:46
  • @JeffC Though one of the solution tells we can do that way but my question is specifically asking how can we do with that other method – Himanshu Poddar Feb 01 '19 at 16:52
  • What other method? Your question is not clear. You have no code that attempts to use `.getAttribute("value")` and the result. The linked question shows you how to do this. Use that code and it will work. If it doesn't, come back and edit your question with the exact code you have used and what the result was. – JeffC Feb 01 '19 at 17:21
  • @JeffC I can show you so many question that asks "How to do it" even when the OP shows no approach to achieve the solution. I just want to ask one thing tell me if I don't know then how do you even expect me to try ? I mean its not I have tried I searched all the related questions on SO and tried all of that. If its not working then how can I post my wrong approach which is not even related. – Himanshu Poddar Feb 01 '19 at 17:29
  • If you don't know how, you search and read. You find some things that might work and you try them. One thing that is NOT OK on SO is to ask questions with no attempts and no explanation. You might be able to link posts that do this but they are likely several years old... even if they aren't, that's not an excuse to repeat wrong behavior. – JeffC Feb 01 '19 at 17:50

1 Answers1

2

You can get the values by using the 'value' attribute of the input node, try the below code :

from selenium import webdriver
from time import sleep

wd = webdriver.Chrome('C:\NotBackedUp\chromedriver.exe')
wd.get('https://www.latlong.net/convert-address-to-lat-long.html')
inputElement = wd.find_element_by_xpath('//input[@placeholder="Type address here to get lat long"]')
inputElement.send_keys('Domlur, Bangalore')
wd.find_element_by_id('btnfind').click()

sleep(8)
print(wd.find_element_by_id('lat').get_attribute('value'))
print(wd.find_element_by_id('lng').get_attribute('value'))

All you need to do is, you need to give some delay before fetching the value. I have used the sleep() method but it is not recommended way to use, you can try some other waits as well. I hope it helps...

Ali
  • 1,689
  • 1
  • 5
  • 12