1

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)

4 Answers 4

2

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"))
Sign up to request clarification or add additional context in comments.

Comments

1

I would like to automate the navigation on google earth with python. By opening the menu then the project and in order to "create a project"

import time
from selenium import webdriver

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait


driver=webdriver.Chrome(executable_path="chromedriver.exe")
driver.get("https://earth.google.com/web/")
print(driver.title)

time.sleep(35)

menu= driver.find_element_by_xpath('//*[@id="menu"]')
menu.click()

2 Comments

This is great that you included code in the answer. It helps to give explanations of why you choose a certain path and what are the trade offs and advantages of your chosen code examples.
My goal is to enter google earth. The algorithm will have to allow me to load the enter page in menu then project and open a KML file, which I could choose. I'm stuck in button control. Do you have an idea.
0

Moin Moin,

I think your problem is based on the fact that the Google Earth web has multiple nested shadowRoots (sub DOMs). This means that you will have to first identify and access the parent DOM(s) within the hierarchy tree in order to access the element in question ('pointer-coordinates').

Here is the javascript needed to access the element you want. You can adapt it to your code:

document.body.children[1].shadowRoot.getElementById("drawer-panel").getElementsByTagName("earth-view-status")[0].shadowRoot.getElementById("pointer-coordinates");

Everytime you see the shadowRoot, you are basically accessing a new sub DOM.

Comments

0

This element is shadow between 2 DOM Element. Use below code which worked for me

public void getCoordinates()  {
    try{
    Thread.sleep(1000);
        }catch (InterruptedException e){

    }
    WebElement shadowDomElementHost0 = driver.findElement(By.cssSelector("earth-app")).element();
    WebElement last0 = (WebElement)((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot",shadowDomElementHost0);
    try{
        Thread.sleep(1000);
    }catch (InterruptedException e){

    }
    WebElement shadowDomElementHost1= last0.findElement(By.cssSelector("earth-view-status[role='toolbar']"));
    WebElement last1 = (WebElement)((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot",shadowDomElementHost1);
    try{
        Thread.sleep(1000);
    }catch (InterruptedException e){

    }
    String Coord=last1.findElement(By.cssSelector(Coordinates)).getText();
    logger.info(Coord);


}

1 Comment

Please note i have seaprate block of code for driver.get("earth.google.com/web") which is not included here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.