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.
1 | from selenium import webdriver |
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,
1 | from selenium import webdriver |
Then fire up the browser and load a webpage.
1 | driver = webdriver.Firefox() |
When the page has loaded, you can take a screenshot using the method .get_screenshot_as_file(filename).
1 | 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 | #coding=utf-8 |
If you are new to selenium, then I highly recommend this book.