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:
JavaScript
x
20
20
1
import mouse
2
import keyboard
3
import time
4
from selenium import webdriver
5
6
options = webdriver.ChromeOptions()
7
options.add_experimental_option("excludeSwitches", ["enable-logging"])
8
driver = webdriver.Chrome(options=options)
9
10
driver.get('https://earth.google.com/web')
11
12
while True:
13
if mouse.is_pressed(button='left') and keyboard.is_pressed('shift'):
14
coordinates = driver.find_elements_by_id('pointer-coordinates')
15
if len(coordinates) > 0:
16
print(coordinates[0].text)
17
else:
18
print('No coordinates found!')
19
time.sleep(0.2)
20
Advertisement
Answer
The element is inside shadow root element you need to use query selector to identify the element.Induce javascript executor.
JavaScript
1
8
1
import time
2
3
driver.get("https://earth.google.com/web")
4
time.sleep(10)
5
corordinate=driver.execute_script("return document.querySelector('earth-app').shadowRoot.querySelector('earth-view-status').shadowRoot.querySelector('span#pointer-coordinates')")
6
print(corordinate.text)
7
print(corordinate.get_attribute("textContent"))
8