How can I click on Google Maps and capture a data point using Selenium?

Viewed 1187

I'm testing this simple code. It gets me to the lat & lon coordinates and zooms in to the point I want.

UPDATED CODE:

import pandas as pd
df_fin = pd.read_csv('C:\\Users\\df_fin.csv')

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time

driver = webdriver.Chrome('C:\\Users\\TryMe\\chromedriver.exe')
driver.maximize_window()
driver.implicitly_wait(5)

i = 1

for item in df_fin['place']:
    try:
        driver.get(item)
        wait = WebDriverWait(driver, 10)
        main_canvas = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[name()='canvas']")))
        size = main_canvas.size
        w, h = size['width'], size['height']
        new_w = w/2
        new_h = h/2
        ActionChains(driver).move_by_offset(new_h, new_h).pause(5).perform()
        time.sleep(2)
        wait.until(EC.element_to_be_clickable((By.XPATH, "//*[name()='canvas']"))).click()
        print(driver.title)
        df_fin['my_place'] = driver.title
    except:
        df_fin['my_place'] = 'OTHER'

    i = i + 1
    print(i)

df_fin.head(10)

That gives me this. The title says 'Google Maps'.

enter image description here

If I click the middle of the screen, I get this.

enter image description here

Now the title says 'Rod Laver Arena - Google Maps'. So, my question is twofold, how can I get some code to click on the center of the map and scrape the title element?

Items in the 'place' field look like this.

https://www.google.com/maps/@42.33988,-71.10409,18z
https://www.google.com/maps/@39.73914,-75.54937,18z
https://www.google.com/maps/@44.4995,-88.05496,18z
https://www.google.com/maps/@44.50235,-88.06322,18z
https://www.google.com/maps/@40.82265,-73.40959,18z
1 Answers

Basically it is a canvas element, using //*[name()='canvas'] we can locate the element, and by default selenium always click in middle of the web element. Also, looks like you can not perform direct click instead it should be through ActionChains, below code I have tried in my local and it seems to be working fine.

Code :

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.google.com/maps/@-37.8217121,144.9786207,18z")
print(driver.title)
wait = WebDriverWait(driver, 10)

main_canvas = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[name()='canvas']")))
size = main_canvas.size
w, h = size['width'], size['height']
new_w = w/2
new_h = h/2
ActionChains(driver).move_by_offset(new_h, new_h).pause(1).perform()
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[name()='canvas']"))).click()
time.sleep(1)
print(driver.title)

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

output :

"C:\Program Files\Python39\python.exe" C:/Users/***/PycharmProjects/SeleniumSO/Chrome.py
Google Maps
Rod Laver Arena - Google Maps

Process finished with exit code 0
Related