Take Webpage Screenshot with Python Selenium
Screenshots of webpages can be taken automatically with Python Selenium Web Driver. First load the selenium module and time module. You need the time module to wait for page loading to complete.
Then once the page is loaded, take the screenshot. This can be a png file or another image format. Then close the web browser, otherwise it will stay open indefinetly.
Related course:
Selenium screenshot
Example
Before you start, make sure that you have the Selenium Web Driver installed (unique for your Web Browser) and that you have the selenium module installed.
You can take a screenshot of a webpage with the method get_screenshot_as_file() with as parameter the filename.
The program below uses firefox to load a webpage and take a screenshot, but any web browser will do.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get(‘https://www.python.org’)
sleep(1)
driver.get_screenshot_as_file(«screenshot.png»)
driver.quit()
print(«end. «)
The screenshot image will be stored in the same directory as your Python script. Unless you explicitly define the path where the screenshot has to be stored.
The first step is to import the required modules,
from selenium import webdriver
from time import sleep
Then fire up the browser and load a webpage.
driver = webdriver.Firefox()
driver.get(‘https://www.python.org’)
sleep(1)
When the page has loaded, you can take a screenshot using the method .get_screenshot_as_file(filename).
driver.get_screenshot_as_file(«screenshot.png»)
Take screenshot of full page with Python Selenium
The above code only takes a screenshot of the visible browser window. There are several ways to take a full page screenshot, which includes the web page from top to bottom.
You can do it this way, note that it’s mandatory to set the browser to headless for this to work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#coding=utf-8
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)
URL = ‘https://pythonbasics.org’
driver.get(URL)
S = lambda X: driver.execute_script(‘return document.body.parentNode.scroll’+X)
driver.set_window_size(S(‘Width’),S(‘Height’)) # May need manual adjustment
driver.find_element_by_tag_name(‘body’).screenshot(‘web_screenshot.png’)
driver.quit()
selenium find element by id
rverton / chrome_headless_screenshot.py
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] « |
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 ]) |
jefftriplett / capture.py
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 () |