How to Automate Website Screenshots

Taking screenshots of websites is a common task for many professionals, from web designers and developers to marketers and content creators. Screenshots are useful for showcasing your work, documenting bugs, creating tutorials, and much more. However, manually capturing screenshots can be time-consuming, especially if you need to take many screenshots of different pages or at different screen sizes.
Fortunately, there are tools and techniques you can use to automate the process of taking website screenshots. In this post, we'll explore how to efficiently capture screenshots using APIs, libraries, and other automation methods.

Why Automate Screenshots?

Before diving into the how, let's discuss the benefits of automating your website screenshot workflow:
  • Save time - Manually navigating to web pages, waiting for them to load, and capturing screenshots is tedious. Automation allows you to capture dozens or hundreds of screenshots in a fraction of the time.
  • Ensure consistency - With manual screenshots, there may be slight variations in window size, scroll position, etc. Automated tools allow you to specify exact dimensions and capture options for pixel-perfect consistency across all screenshots.
  • Improve accuracy - Automated screenshot tools can capture the full length of a page, even content below the fold that would be missed with a regular screenshot. They can also capture elements after the page fully loads to avoid missing late-rendered content.
  • Integrate with other tools - Screenshot APIs and libraries can be combined with other automation tools and scripts as part of a broader workflow. For example, automatically capturing screenshots after deploying an update to your website.

Website Screenshot APIs

One of the easiest ways to automate website screenshots is by using a dedicated screenshot API service. These tools provide a simple REST API that you can call to generate screenshots of any public website. Let's take a look at one of the most popular options.

ScreenshotOne API

ScreenshotOne is a powerful and full-featured screenshot API that can render any website or HTML into images (PNG, JPG, WebP) and PDFs. The API is very flexible, with options to specify screen size, emulate mobile devices, set custom CSS, delay capture, block cookie banners, and much more.
To use ScreenshotOne, you first need to sign up for a free API key. Then you can make a GET or POST request to the API endpoint with the URL of the page you want to capture. For example:
https://api.screenshotone.com/take?access_key=ACCESS_KEY&url=https://example.com
This will return the screenshot image in the response. ScreenshotOne provides a generous free tier, with paid plans available for higher volume needs.
Some key features of ScreenshotOne include:
  • Blocking of cookie banners, ads, and other unwanted elements
  • Specifying custom CSS and JavaScript to modify page rendering
  • Setting custom headers and cookies
  • Generating PDFs with multiple pages
  • Returning screenshots in JSON format with additional metadata
  • Capturing screenshots of specific HTML elements using CSS selectors
ScreenshotOne has a well-documented API with many code examples. They also offer a Zapier integration and several official API clients for Node.js, Python, PHP, and more.

Automated Screenshot Libraries

If you prefer to run the screenshot capturing on your own infrastructure, there are several open source libraries that provide automated website screenshot functionality. These typically use a headless browser like Puppeteer to load the web page and take the screenshot.

Puppeteer

Puppeteer is a popular Node.js library for controlling a headless Chrome browser. It has built-in support for capturing screenshots of web pages. Here's a basic example:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); await page.screenshot({path: 'example.png'}); await browser.close(); })();
This script launches a headless Chrome instance, navigates to the specified URL, captures a screenshot, and then closes the browser. You can customize the screenshot by passing additional options to the screenshot method, such as:
  • fullPage
    • Capture the full scrollable page, not just the visible viewport
  • clip
    • Capture a specific rectangular area of the page
  • omitBackground
    • Omit the page's background, yielding a transparent screenshot
Puppeteer has many other methods for interacting with the page, such as clicking buttons, filling out forms, and waiting for elements to appear. This allows you to capture screenshots of pages that require interaction or that lazy-load content.
The downside of Puppeteer is that you need to run and manage the headless Chrome process yourself. This can be more complex to deploy and scale compared to a hosted API solution.

Playwright

Playwright is a newer alternative to Puppeteer that supports multiple browser engines (Chromium, Firefox, and WebKit). It has a very similar API to Puppeteer. Here's how to take a screenshot with Playwright:
const { chromium } = require('playwright'); (async () => { const browser = await chromium.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); await page.screenshot({ path: 'example.png' }); await browser.close(); })();
The main advantage of Playwright is cross-browser support. This is useful if you need to test how your site renders in different browsers. Playwright also has some additional features like mobile emulation and network throttling.
However, like Puppeteer, you are responsible for provisioning and managing the browser infrastructure with Playwright.

Automated Screenshot CLIs

Another option for automated screenshots is using a command-line interface (CLI) tool. These are useful for quickly capturing screenshots from the terminal or for integrating with shell scripts. Here are a couple popular screenshot CLI tools.

Pageres

Pageres is a cross-platform CLI for capturing screenshots of websites in various resolutions. It is built on top of Puppeteer. Here's a basic example of using Pageres:
npx pageres https://example.com 1366x768 --crop
This will capture a 1366x768 screenshot of example.com, cropped to the specified dimensions. You can also specify multiple screen sizes and formats:
npx pageres https://example.com 800x600 1024x768 --format=jpg
Pageres has a variety of useful options, such as a delay before capturing, custom CSS/JS injection, authentication, and more. Refer to the documentation for details.

Capture Website

Capture Website is another CLI by the same author as Pageres. It is a more flexible, lower-level tool also built with Puppeteer. Here's how to use it:
npx capture-website https://example.com --output=example.png
This will save a full-page PNG screenshot of example.com. Capture Website supports many of the same options as Pageres, but also allows for specifying a custom Puppeteer script for more advanced use cases.

Conclusion

Automated website screenshots are a huge time-saver and can be integrated into various workflows. For the simplest solution, use a hosted screenshot API service like ScreenshotOne. For more control and flexibility, try an automated browser library like Puppeteer or Playwright. And for quick CLI usage or shell scripting, Pageres and Capture Website are great options.
Whichever tool you choose, automating your screenshot process will help you be more productive and efficient in your web development work. You'll never want to manually capture screenshots again!
Taking screenshots should not be difficult, that's why we built Xnapper. Xnapper is a powerful screenshot app for Mac that lets you capture screenshots, annotate images, and share them with ease. Try it out today and see how it can streamline your screenshot workflow!
Ilias Ism

Article written by

Ilias Ism

Ilias helps teams improve their marketing with Xnapper