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

# navigate to the application home page
maps = driver.find_element_by_xpath("//*[@id='listing-details-tabs']/ul/li[2]")


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 - 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")

and the error is,

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


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"
  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