Created
July 19, 2012 18:59
-
-
Save cnf/3146008 to your computer and use it in GitHub Desktop.
Make screenshots of areas in selenium
This file contains 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/env python | |
from PIL import Image | |
import StringIO | |
def screenshot(browser, element, filename='screenshot.png'): | |
"""docstring for screenshot""" | |
elem = browser.find_element_by_css_selector(element) | |
loc = elem.location | |
size = elem.size | |
screenshot = browser.get_screenshot_as_base64() | |
img = Image.open(StringIO.StringIO(screenshot.decode('base64'))) | |
# box = (left, upper, right, lower) | |
left = loc['x'] | |
top = loc['y'] | |
width = size['width'] | |
height = size['height'] | |
box = (left, top, left+width, top+height) | |
area = img.crop(box) | |
area.save("images/{}".format(filename), 'png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment