Search code examples
pythonseleniumweb-scrapinggoogle-earth

Google Earth scraping using python selenium


I want to create a web scraper for earth.google.com/web. Whenever the user clicks while holding shift button, the script will print the coordinates which are displayed at the bottom right corner of the google earth web page.

I am using selenium with chromedriver but it cannot find the coordinates web element. I have tried css selector, xpath, full x-path, find by id. Nothing worked.

Here is my code:

import mouse
import keyboard
import time
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options)

driver.get('https://earth.google.com/web')

while True:
    if mouse.is_pressed(button='left') and keyboard.is_pressed('shift'):
        coordinates = driver.find_elements_by_id('pointer-coordinates')
        if len(coordinates) > 0:
            print(coordinates[0].text)
        else:
            print('No coordinates found!')
        time.sleep(0.2)

Solution

  • The element is inside shadow root element you need to use query selector to identify the element.Induce javascript executor.

    import time
    
    driver.get("https://earth.google.com/web")
    time.sleep(10)
    corordinate=driver.execute_script("return document.querySelector('earth-app').shadowRoot.querySelector('earth-view-status').shadowRoot.querySelector('span#pointer-coordinates')")
    print(corordinate.text)
    print(corordinate.get_attribute("textContent"))