Skip to content

Instantly share code, notes, and snippets.

@siso
Created June 5, 2015 04:40
Show Gist options
  • Save siso/268c19a7b7fe601ef036 to your computer and use it in GitHub Desktop.
Save siso/268c19a7b7fe601ef036 to your computer and use it in GitHub Desktop.
Take SVG screenshot
#!/usr/bin/env python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image
browser = webdriver.Firefox()
try:
# load the page
browser.get('http://127.0.0.1:8080/#radekstepan/disposable/2')
# wait until the page is loaded, or 10s
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "chart"))
)
# take screenshot
browser.save_screenshot('screenshot.png')
# now that we have the preliminary stuff out of the way time to get that image :D
element = browser.find_element_by_id('content') # find part of the page you want image of
location = element.location
size = element.size
im = Image.open('screenshot.png') # uses PIL library to open image in memory
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
finally:
browser.quit()
@siso
Copy link
Author

siso commented Jun 5, 2015

Run pip install Pillow to install PIL on OS X.

@asaboor-gh
Copy link

Where is svg in all this? Just PNG

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