Created
July 10, 2017 08:53
-
-
Save rverton/d07a2232f4c0e1c2b9894e9bdb4fa6cf to your computer and use it in GitHub Desktop.
Make a screenshot with a headless google chrome in python
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
# Install chromedriver from https://sites.google.com/a/chromium.org/chromedriver/downloads | |
import os | |
from optparse import OptionParser | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
CHROME_PATH = '/usr/bin/google-chrome' | |
CHROMEDRIVER_PATH = '/usr/bin/chromedriver' | |
WINDOW_SIZE = "1920,1080" | |
chrome_options = Options() | |
chrome_options.add_argument("--headless") | |
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE) | |
chrome_options.binary_location = CHROME_PATH | |
def make_screenshot(url, output): | |
if not url.startswith('http'): | |
raise Exception('URLs need to start with "http"') | |
driver = webdriver.Chrome( | |
executable_path=CHROMEDRIVER_PATH, | |
chrome_options=chrome_options | |
) | |
driver.get(url) | |
driver.save_screenshot(output) | |
driver.close() | |
if __name__ == '__main__': | |
usage = "usage: %prog [options] <url> <output>" | |
parser = OptionParser(usage=usage) | |
(options, args) = parser.parse_args() | |
if len(args) < 2: | |
parser.error("please specify a URL and an output") | |
make_screenshot(args[0], args[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment