Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in DevOps and Agile by (19.7k points)

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 a 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?

1 Answer

0 votes
by (62.9k points)

PhantomJS

var page = require('webpage').create(); page.open('http://github.com/', function () { page.render('github.png'); phantom.exit(); });

This is how to get a screenshot in phantomJS, I've used phantomJS for some time now.

You can find more information here.

Selenium

driver = webdriver.Chrome(); driver.get('http://www.google.com'); driver.save_screenshot('out.png'); driver.quit();

Hope this helps.

Browse Categories

...