Reputation: 59
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)
Upvotes: 1
Views: 982
Reputation: 11
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()
Upvotes: 1
Reputation: 1026
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);
}
Upvotes: 0
Reputation: 33384
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"))
Upvotes: 2
Reputation: 80
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.
Upvotes: 0