Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Last active August 29, 2015 14:16
Show Gist options
  • Save kezabelle/21422e89614b330ab234 to your computer and use it in GitHub Desktop.
Save kezabelle/21422e89614b330ab234 to your computer and use it in GitHub Desktop.
Taking screenshots of URLs at various resolutions, using selenium & python. Assumes Django, so tries to remove django-debug-toolbar as necessary.
#! /usr/bin/env python
from sys import exit
from datetime import datetime
from itertools import product
from selenium import webdriver
RESOLUTIONS = (
(360, 480),
(400, 600),
(480, 320),
(600, 400),
)
URLS = (
'http://localhost:8080/',
)
if __name__ == '__main__':
browser = webdriver.Firefox()
remove_debug_toolbar_if_exists = """
var djdebug_exists = document.getElementById('djDebug');
if (djdebug_exists === void(0) || djdebug_exists === null) {}
else {
djdebug_exists.remove();
}
"""
try:
for window_size, url in product(RESOLUTIONS, URLS):
browser.set_window_size(*window_size)
browser.get(url)
browser.execute_script(remove_debug_toolbar_if_exists)
wxh = '{0!s}x{1!s}'.format(*window_size)
filename = '{dims!s}_{dt!s}.png'.format(dims=wxh,
dt=datetime.today())
browser.get_screenshot_as_file(filename)
finally:
browser.quit()
exit(0)
@kezabelle
Copy link
Author

Converted to a full repository at https://github.com/kezabelle/screenslop using http://stackoverflow.com/a/13671484 as a guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment