简体   繁体   English

使用 Selenium 和 Google Maps API 的屏幕截图地图

[英]Screenshot Map using Selenium and Google Maps API

I am trying to use Selenium, PhantomJS, and GoogleMaps API to automatically screenshot / save down maps.我正在尝试使用 Selenium、PhantomJS 和 GoogleMaps API 来自动截屏/保存地图。 The url i request is a local html file with javascript to generate the map.我请求的 url 是一个带有 javascript 的本地 html 文件来生成地图。 When I open the local file, I am able to view the map, however, when I run the following code and attempt to screenshot the map, only a blank picture is saved.当我打开本地文件时,我可以查看地图,但是,当我运行以下代码并尝试对地图进行截图时,只保存了一张空白图片。

I have explored the Google Static Maps API, but my maps have hundreds of markers, exceeding the URL length limit.我已经探索了 Google Static Maps API,但我的地图有数百个标记,超出了 URL 长度限制。 I am trying to screenshot hundreds of maps that will change over time and need to be a set size.我正在尝试对数百张地图进行截图,这些地图会随着时间的推移而变化,并且需要设定大小。 I believe this is the best way to go about it.我相信这是最好的方法。

As reference, here is some test html that brings up a Google API map (note: will require API key): https://developers.google.com/maps/documentation/javascript/earthquakes作为参考,这里有一些测试 html,可以显示 Google API 地图(注意:需要 API 密钥): https : //developers.google.com/maps/documentation/javascript/earthquakes

Here is my code:这是我的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def maps():
    driver = webdriver.PhantomJS()
    driver.set_window_size(640,640)
    driver.implicitly_wait(5)
    driver.get("file:///U:/ABC%20Comps/test.html")    
    element = driver.find_element_by_id('map')
    driver.save_screenshot('test.png')
    driver.quit()

` `

Can someone point me in the right direction?有人可以指出我正确的方向吗?

You should be wait for the page to load all the AJAX requests and render them.您应该等待页面加载所有 AJAX 请求并呈现它们。

The normal workflow for Selenium is wait for all the normal requests to finish, then go to the next line. Selenium 的正常工作流程是等待所有正常请求完成,然后转到下一行。 BUT for some cases, this isn't how things use to happen, due to asynchronous requests etc.但是在某些情况下,由于异步请求等,这不是事情发生的方式。

Below, I'm showing an working example.下面,我展示了一个工作示例。 What it does is, that it forces the script to wait for the number of requests to stop getting bigger, then wait some extra 5 seconds (for better render) and THEN take the screenshot.它的作用是强制脚本等待请求数量停止变大,然后再等待 5 秒(为了更好的渲染),然后截取屏幕截图。 Here it is, have fun !在这里,玩得开心! :

if platform.system() == 'Windows' :
        executables_extension='.exe'
    else :
        executables_extension=''

    options = FirefoxOptions()
    options.add_argument("--headless")
    browser = webdriver.Firefox(options=options, executable_path=os.path.dirname(os.path.realpath(__file__))+os.path.sep+'geckodriver'+executables_extension)
    requests = 0
    requests_stop = False
    browser.get(Extracted_URL)
    while requests_stop == False :
        requests_old = requests
        time.sleep(3)
        requests = browser.execute_script("var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network.length;")
        if requests_old == requests :
            time.sleep(5)
            requests = browser.execute_script("var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network.length;")
            if requests_old == requests :
                requests_stop = True

    browser.save_screenshot('screenie.png')
    time.sleep(1)
    browser.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM