Skip to content

Instantly share code, notes, and snippets.

@jefftriplett
Last active November 10, 2017 17:25
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 jefftriplett/132b1fa498d1bb0283cfe40ff8fc5ff5 to your computer and use it in GitHub Desktop.
Save jefftriplett/132b1fa498d1bb0283cfe40ff8fc5ff5 to your computer and use it in GitHub Desktop.
This might help anyone struggling with webkit2png which is outdated and broken on a lot of websites. This python script uses headless chrome and only scratches the surface of what is possible. Thanks to https://gist.github.com/rverton/d07a2232f4c0e1c2b9894e9bdb4fa6cf for inspiration.
# Adapted from: https://gist.github.com/rverton/d07a2232f4c0e1c2b9894e9bdb4fa6cf
#
# Install:
# chromedriver from https://sites.google.com/a/chromium.org/chromedriver/downloads
# Python deps (pip or pipenv): `pipenv install click selenium`
#
# To use:
# python chrome_headless_screenshot.py --height=1280 https://jefftriplett.com/ screenshot.png
import click
from selenium import webdriver
CHROME_PATH = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
CHROMEDRIVER_PATH = '/usr/local/bin/chromedriver'
@click.command()
@click.option('--width', '-W', default=1024)
@click.option('--height', '-H', default=1024)
@click.argument('url')
@click.argument('output')
def make_screenshot(url, output, width, height):
if not url.startswith('http'):
raise Exception('URLs need to start with "http"')
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size={width},{height}".format(width=width, height=height))
chrome_options.binary_location = CHROME_PATH
driver = webdriver.Chrome(
executable_path=CHROMEDRIVER_PATH,
chrome_options=chrome_options
)
driver.get(url)
driver.save_screenshot(output)
driver.close()
if __name__ == '__main__':
make_screenshot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment