-
-
Save stungkit/012036f395d5b508b649045297c87127 to your computer and use it in GitHub Desktop.
Using Python 3, selenium, and headless chrome ala chromedriver to capture website screenshots
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
""" | |
To install: | |
# python requirements | |
$ pip install click selenium | |
# for headless chrome | |
$ brew install chromedriver | |
To use: | |
$ python capture.py https://revsys.com revsys.png | |
""" | |
import click | |
import time | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
@click.command() | |
@click.option('--delay', default=1, help='Delay before capturing screenshot') | |
@click.option('--headless/--no-headless', default=True, help='Run in headless mode') | |
@click.option('--height', default=512, help='Browser height') | |
@click.option('--width', default=1024, help='Browser width') | |
@click.option('--zoom', default=100, help='Zoom level') | |
@click.argument('url') | |
@click.argument('filename') | |
def capture(url, filename, delay, headless, height, width, zoom): | |
chrome_options = Options() | |
if headless: | |
chrome_options.add_argument('--headless') | |
chrome_options.add_argument(f'window-size={width}x{height}') | |
driver = webdriver.Chrome( | |
'/usr/local/bin/chromedriver', chrome_options=chrome_options | |
) | |
driver.get(url) | |
if zoom != 100: | |
driver.execute_script(f"document.body.style.zoom='{zoom}%'") | |
time.sleep(delay) | |
driver.save_screenshot(filename) | |
driver.quit() | |
if __name__ == '__main__': | |
capture() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment