Written By Hanzala Saleem
Updated At July 03, 2026 | 6 min read
There are four common ways to take a screenshot in Python: pyautogui for the desktop screen, Selenium or Playwright for rendering a webpage in a real browser, and a screenshot API when you want captures without running a browser yourself. Which one you pick depends on whether you are capturing your own screen or a live webpage, and whether you want to manage browser infrastructure.
The fastest way to screenshot a webpage in Python is Playwright: install it, open the URL, and call page.screenshot(full_page=true). If you do not want to install or scale browsers, send the URL to a screenshot API and get the image back in one request.
This guide walks through all four methods with working code, then compares them so you can choose the right one for your project.
What "taking a screenshot in Python" means: It is capturing a rendered image, either of your local desktop screen or of a live webpage, and saving it as a file such as PNG, JPG, or PDF. Desktop capture uses a screen-grab library like pyautogui. Webpage capture uses a headless browser (Selenium or Playwright) or a hosted screenshot API that renders the page and returns the image.
| Method | Best for | What it captures | Full-page webpage support | Browser setup |
|---|---|---|---|---|
| pyautogui | Grabbing whatever is on your local screen | The visible desktop | No | None, it photographs the screen |
| Selenium | Teams already running Selenium in their stack | A rendered webpage | Limited, needs extra code | You install and manage a WebDriver |
| Playwright | Modern webpage capture inside your own code | A rendered webpage | Yes, full_page=True | You install and manage browsers |
| ScreenshotAPI | Automated or high-volume capture with no infrastructure | A rendered webpage | Yes, full_page=true | None, fully hosted |
Rule of thumb: if you are capturing a live URL and do not want to babysit browsers, use an API. If you need scripted interaction before the capture, use Playwright or Selenium. If you literally want a photo of your own screen, use pyautogui.
If you want to take screenshots the traditional Python way, you’ll first need to import the pyautogui module:
pip install pyautoguiOnce you’ve imported the package, decide where you want the screenshot to be saved.
For example, let’s use the following location:
C:\Users\User\Desktop\Screenshots\screenshot.pngYou’ll call the screenshot function to take a screenshot, save it as a variable, and then use the save function to save the screenshot in the location mentioned earlier:
exampleScreenshot = pyautogui.screenshot()
exampleScreenshot.save(r'C:\Users\User\Desktop\Screenshots\screenshot.png')On Linux, pyautogui needs the scrot utility installed; on macOS it uses the built-in screencapture command. Because it only sees your local screen, pyautogui cannot capture a website you have not manually opened and scrolled to. For webpages, a headless browser or an API is the right tool.
Playwright is the modern default for capturing a webpage in Python. It runs a real headless browser, renders JavaScript, and supports full-page screenshots with a single option.
Install Playwright and its browser binaries:
pip install playwright
playwright install chromiumOpen a URL and save a full-page screenshot:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page(viewport={"width": 1920, "height": 1080})
page.goto("https://example.com", wait_until="networkidle")
page.screenshot(path="screenshot.png", full_page=True)
browser.close()The wait_until="networkidle" setting tells Playwright to wait until network activity settles, which helps lazy-loaded images and dynamic content finish rendering before capture. Set full_page=true to stitch the entire scrollable page into one image, or leave it off to capture just the viewport. To screenshot a single element, target it with a locator and call its screenshot method instead of the page.
Selenium is a good fit when your project already uses it for testing or automation. It also drives a real browser, so it renders JavaScript before capture. Full-page capture takes more work than Playwright, but basic viewport screenshots are straightforward.
Install Selenium:
pip install seleniumCapture a webpage in headless Chrome:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless=new")
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(options=options)
driver.get("https://example.com")
driver.save_screenshot("screenshot.png")
driver.quit()save_screenshot captures the current viewport. For a true full-page image you either resize the window to the full document height before capturing, or use a helper library that stitches multiple viewport captures together. This extra handling is the main reason many developers prefer Playwright or a hosted API for full-page work.
With ScreenshotAPI, you can fully automate taking screenshots. Regardless of the format (from PNG to PDF) or the volume, you’ll get high-quality screenshots every time.
When taking screenshots with ScreenshotAPI, just import the parse and request modules of Python’s urllib package. Use the following code:
import urllib.parse
import urllib.requestThen, import Python’s ssl module that gives you access to Transport Layer Security (also known as TLS, Secure Sockets Layer, or SSL) encryption and peer authentication facilities for network sockets. Here, you’ll use:
import sslNext, create an unverified SSLContext object with the ssl._create_unverified_context() function:
ssl._create_default_https_context = ssl._create_unverified_contextSet the variables that you’ll use to construct your query parameters, including:
● The string containing your URL key
token = "Your ScreenshotAPI API Key"● The encoded URL string containing the URL you’re targeting.
url = urllib.parse.quote_plus(" https://google.com ")● The integer indicating the width of your target render.
width = 1920
● The integer indicating the height of your target render.
height = 1080● The string specifying the output format, “image” or “json”.
output = "image"Once you’ve set the variables, you can construct your query parameters and URL with the following code:
query = "https://shot.screenshotapi.net/screenshot"
query += "?token=%s&url=%s&width=%d&height=%d&output=%s" % (token, url, width, height, output)Using the query parameters above, you can then call the API:
urllib.request.urlretrieve(query, "./screenshot.png")For every screenshot you want to take, you’ll then only need to change the URL variable to get screenshots of different web pages.
A viewport screenshot captures only the area visible in the browser window at the set width and height. A full-page screenshot captures the entire scrollable document, top to bottom, as one tall image.
With Playwright, switch between them using the full_page option: full_page=true for the whole document, or omit it for the viewport. With the ScreenshotAPI request above, set full_page to "true" or "false". With Selenium, viewport capture is the default and full-page capture needs extra scrolling or stitching logic.
Full-page capture on modern sites can miss lazy-loaded images or break on infinite-scroll pages, because content below the fold may not have loaded yet. Waiting for network activity to settle before capture, as shown in the Playwright example, reduces blank or partial images.
For a quick photo of your own screen, pyautogui is enough. For capturing live webpages inside your own code, Playwright is the best default, with Selenium a reasonable choice if it is already in your stack. When screenshots are a feature of your product and you do not want to run browsers at scale, a hosted screenshot API is the lowest-maintenance path.
If that last case sounds like yours, you can capture any URL with a single request using ScreenshotAPI. The free plan includes 100 screenshots per month with no credit card required.
The easiest way is Playwright: install it, open the URL, and call page.screenshot(full_page=true). It renders JavaScript in a real browser, so dynamic pages capture correctly. If you would rather not run a browser at all, send the URL to a screenshot API and receive the image in a single request.
No. pyautogui captures whatever is currently on your desktop screen, not a webpage. It cannot open URLs or render HTML. To screenshot a website you need a headless browser such as Playwright or Selenium, or a hosted screenshot API that renders the page for you.
In Playwright, pass full_page=true to page.screenshot(). With the ScreenshotAPI request, set full_page to "true". In Selenium, you must resize the window to the full document height or stitch multiple viewport captures, which is why many developers prefer Playwright or an API for full-page work.
Only if you run the capture yourself. Selenium and Playwright both drive a headless browser locally, which means you install and maintain it. A screenshot API removes that step by running Chromium on its own infrastructure and returning the finished image.
Loop over a list of URLs and call a screenshot API for each one, saving each response to a file. Because the API runs the browser for you, there is no driver to install, no memory to tune, and no crash recovery to handle when volume grows.