Roy Roy - 24 days ago 9
Python Question

Python Web scraping - How to locate the Co-ordinates of the map element?

I am doing a bit of web scraping and using selenium with python. I am not able to locate a map element and get the latitude and longitude from it. Can anyone have a look at the code. This is my code,

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time

# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()

# navigate to the application home page
driver.get("http://www.zoopla.co.uk/to-rent/commercial/details/41716558?search_identifier=c2df4fb926a1dd55be2a0e31c706380b#1cy1zTwJA1aKrxcV.97")
maps = driver.find_element_by_xpath("//*[@id='listing-details-tabs']/ul/li[2]")
maps.click()

laltong=driver.find_element_by_xpath("//*[@id='bto-rent-commercial']/script[6]/text()")
print(latlong.text)


I understand that my xpath for locating the element is totally wrong, because I am not even able to locate that element. Any suggestions on this ?
Here is a link to the webpage - http://www.zoopla.co.uk/to-rent/commercial/details/42080041?search_identifier=f02093ba0369b1204ac8cf6369e419f5#RuZu0HmxXSL3auBJ.97 and click on Map and nearby.

The latest code that i tried is,

laltong=driver.find_element_by_xpath("//*[@title='Click to see this area on Google Maps']/div/div[1]/a")
print(latlong.get_attribute("href"))


and the error is,

Unable to locate element: {"method":"xpath","selector":"//*[@title='Click to see this area on Google Maps']/div/div[1]/a"}

Answer

For those that come later, if you:

  1. navigate to this link
  2. click on the Map & nearby tab
  3. click on the Google logo in the bottom left of the map (white text on light background... it's hard to see)

That will open the map that OP is looking for.

Now with that out of the way... the A tag looks like

<a target="_blank"
  href="https://maps.google.com/maps?ll=51.552574,-0.052437&amp;z=15&amp;t=m&amp;hl=en-US&amp;gl=US&amp;mapclient=apiv3"
  title="Click to see this area on Google Maps"
  style="position: static; overflow: visible; float: none; display: inline;">...</a>

where the href attribute is the important bit that we want.

To get this A tag, we can use a CSS selector "a[title='Click to see this area on Google Maps']" to find the element. From there you would get the href attribute, which I'm assuming you know how to do already.


After your update, I see that you are using XPath. Your XPath is close but the element that contains title="Click to see this area on Google Maps" is the A that you want so the XPath would be "//a[@title='Click to see this area on Google Maps']". I would suggest you use CSS selectors because they are faster and less prone to errors.

CSS selectors reference

CSS selectors tips