Skip to content

Instantly share code, notes, and snippets.

@leafo
Created March 16, 2014 02:49
Show Gist options
  • Save leafo/9577854 to your computer and use it in GitHub Desktop.
Save leafo/9577854 to your computer and use it in GitHub Desktop.
selenium snapshots
#!/usr/bin/env python2.7
from xml.dom import minidom
import urllib2
import os
import shutil
import random
import re
import sys
num_snapshots = 100
if len(sys.argv) > 1:
num_snapshots = int(sys.argv[1])
out_dir = "selenium_snapshots"
host = "http://localhost.com:8080"
req = urllib2.urlopen(host + "/sitemap.xml")
dom = minidom.parse(req)
print "Taking %d snapshots from %s" % (num_snapshots, host)
shutil.rmtree(out_dir, True)
os.makedirs(out_dir)
def get_text(node):
return node.childNodes[0].data
def url_filename(url):
return re.sub(r'[^\w-]+', "_", url)
urls = [get_text(node) for node in dom.getElementsByTagName("loc")]
random.shuffle(urls)
urls = urls[:num_snapshots]
from selenium import webdriver
browser = webdriver.Firefox()
for url in urls:
print "Running " + url
browser.get(url)
browser.save_screenshot(os.path.join(out_dir, url_filename(url)) + ".png")
browser.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment