Skip to content

Instantly share code, notes, and snippets.

@ishuah
Created January 28, 2014 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishuah/8671610 to your computer and use it in GitHub Desktop.
Save ishuah/8671610 to your computer and use it in GitHub Desktop.
Python/Django - Take screenshots of pages that require authentication with Selenium
#this would be in your view.py
from pyvirtualdisplay import Display
from selenium import webdriver
def take_screenshot(request):
HOST = request.get_host()
display = Display(visible=0, size=(1920,1440))
display.start()
browser = webdriver.Firefox()
#before we can add a cookie, we need to access the page first
browser.get(HOST)
browser.add_cookie({ 'name': 'sessionid', 'value': request.COOKIES['sessionid'], 'path': '/', 'domain': HOST })
#assuming your user is a SuperAdmin
browser.get(HOST+'/admin/')
browser.save_screenshot(MEDIA_ROOT+'/test.png')
browser.quit()
display.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment