Skip to content

Instantly share code, notes, and snippets.

@MasterSwift
Forked from jefftriplett/capture.py
Created September 17, 2019 00:42
Show Gist options
  • Select an option

  • Save MasterSwift/d0b42cc3f99a534328a95b04408df9e4 to your computer and use it in GitHub Desktop.

Select an option

Save MasterSwift/d0b42cc3f99a534328a95b04408df9e4 to your computer and use it in GitHub Desktop.

Revisions

  1. @jefftriplett jefftriplett created this gist Oct 27, 2018.
    54 changes: 54 additions & 0 deletions capture.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    """
    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()