Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python selenium的Google Earth抓取_Python_Selenium_Web Scraping_Google Earth - Fatal编程技术网

使用python selenium的Google Earth抓取

使用python selenium的Google Earth抓取,python,selenium,web-scraping,google-earth,Python,Selenium,Web Scraping,Google Earth,我想为您创建一个web刮板。每当用户在按住shift键的同时单击时,脚本将打印显示在google earth网页右下角的坐标 我正在使用selenium和chromedriver,但它找不到坐标web元素。我尝试了css选择器、xpath、完整x路径、按id查找。没有任何效果 这是我的密码: import mouse import keyboard import time from selenium import webdriver options = webdriver.ChromeOpti

我想为您创建一个web刮板。每当用户在按住shift键的同时单击时,脚本将打印显示在google earth网页右下角的坐标

我正在使用selenium和chromedriver,但它找不到坐标web元素。我尝试了css选择器、xpath、完整x路径、按id查找。没有任何效果

这是我的密码:

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)
云云

我认为你的问题是基于这样一个事实:Google Earth web有多个嵌套的shadowRoots(子DOM)。这意味着您必须首先识别并访问层次结构树中的父DOM,才能访问相关元素(“指针坐标”)

下面是访问所需元素所需的javascript。您可以根据您的代码对其进行调整:

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

每次看到shadowRoot,基本上就是在访问一个新的子DOM。

元素在shadowRoot元素中,您需要使用查询选择器来标识元素。诱导javascript执行器

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"))

此元素是2个DOM元素之间的阴影。使用下面对我有用的代码

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);


}

我想用python在google earth上自动导航。通过打开菜单,然后打开项目并“创建项目”


请注意,我有一个seaprate代码块用于驱动程序。get(“)答案中包含了代码,这很好。这有助于解释为什么选择某个路径,以及所选代码示例的优缺点。我的目标是进入google earth。算法必须允许我在菜单中加载enter页面,然后投影并打开KML文件,我可以选择。我被困在按钮控制中。你有什么想法吗。
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()