简体   繁体   中英

Screenshot Map using Selenium and Google Maps API

I am trying to use Selenium, PhantomJS, and GoogleMaps API to automatically screenshot / save down maps. The url i request is a local html file with javascript to generate the map. 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. 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

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.

The normal workflow for Selenium is wait for all the normal requests to finish, then go to the next line. 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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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