-
-
Save gatspy/14c8f1f51b27d7119fc2f05afdcb4396 to your computer and use it in GitHub Desktop.
make full screenshot with selenium in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from selenium import webdriver | |
from PIL import Image | |
from cStringIO import StringIO | |
verbose = 1 | |
browser = webdriver.Firefox() | |
browser.get('http://stackoverflow.com/questions/37906704/taking-a-whole-page-screenshot-with-selenium-marionette-in-python') | |
# from here http://stackoverflow.com/questions/1145850/how-to-get-height-of-entire-document-with-javascript | |
js = 'return Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);' | |
scrollheight = browser.execute_script(js) | |
if verbose > 0: | |
print scrollheight | |
slices = [] | |
offset = 0 | |
while offset < scrollheight: | |
if verbose > 0: | |
print offset | |
browser.execute_script("window.scrollTo(0, %s);" % offset) | |
img = Image.open(StringIO(browser.get_screenshot_as_png())) | |
offset += img.size[1] | |
slices.append(img) | |
if verbose > 0: | |
browser.get_screenshot_as_file('%s/screen_%s.png' % ('/tmp', offset)) | |
print scrollheight | |
screenshot = Image.new('RGB', (slices[0].size[0], scrollheight)) | |
offset = 0 | |
for img in slices: | |
screenshot.paste(img, (0, offset)) | |
offset += img.size[1] | |
screenshot.save('/tmp/test.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment