Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

To capture a Selenium screenshot with a background, make sure the background is rendered by the page before taking the screenshot. Selenium controls the browser and captures its rendered content; it does not add a background to an image after capture. Set or preserve the page’s CSS, choose a window or element screenshot, set the viewport deliberately, and save the PNG.

How Selenium captures a background

A WebDriver screenshot records rendered browser content. If the desired background is supplied by the page’s CSS and visible in the captured region, it will appear in the screenshot. Selenium’s screenshot methods do not automatically create a background, and its documentation does not promise transparent output or define a universal transparency workflow. For a consistent solid color, image, or gradient, make it part of the rendered page before capture and inspect the saved file.

For a site you own, prefer the application’s stylesheet or test fixture so the background is part of the intended page state. For a controlled test page, you can use Selenium’s JavaScript execution facility to adjust an element’s style before taking the screenshot. That changes the page under test, so it may be inappropriate when the screenshot must represent the unmodified production appearance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose what to capture

Current page or window

Use the current browsing context screenshot when you need the page view as a whole, including surrounding content. The Python save_screenshot(filename) API saves the current window to a PNG file. Selenium’s documentation also demonstrates screenshot capture from the current browsing context. See the Selenium guide to working with windows and tabs.

A selected element

Use an element screenshot for a specific component, such as a card, chart, or logo. This isolates the selected element rather than preserving all the surrounding page context. Selenium’s documented examples show element screenshots as well as broader screenshots. Check the output image: the actual crop and layout may differ from what you expect, particularly if the target has padding, an internal background, or content extending beyond its visible box.

The entire document

A current-window screenshot and a full-document screenshot are different requirements. The reviewed Python Firefox API documents full-document screenshot methods; that does not establish the same capability for every browser and Selenium binding. If you need the whole document, confirm support in the API reference for the specific browser and binding you use rather than assuming a standard screenshot captures content below the viewport.

Python: set a background and save a window screenshot

This example follows the documented Python WebDriver screenshot API. The temporary style targets body only; many sites put their background on a wrapper, root element, or another container instead.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from selenium import webdriver

with webdriver.Firefox() as driver:
    driver.set_window_size(1440, 1000)
    driver.get("https://example.com")

    # For a controlled page only: this changes its rendered state.
    driver.execute_script(
        "document.body.style.backgroundColor = '#f3f4f6';"
    )

    saved = driver.save_screenshot("./screenshot.png")
    if not saved:
        raise OSError("Screenshot could not be saved")

The Python API reference specifies that save_screenshot(filename) saves the current window to a PNG file, expects a full path ending in .png, and returns True on success or False on an I/O error. See the Python remote WebDriver API reference.

Use the right CSS property and target

The example uses backgroundColor for a solid color. A background image or gradient needs an appropriate CSS background or backgroundImage value. If the page already has inline styles, changing only the intended property is generally safer than replacing the whole style attribute. First identify the element that actually paints the background; setting body may not affect a full-page wrapper or a component.

For a site the test does not control, use supported application configuration or fixtures where possible. Injecting JavaScript can make a screenshot look right while invalidating the state the test is supposed to verify.

Wait for the required page state

Navigate, then wait until the observable condition relevant to the screenshot is true: for example, a particular component is present or the dynamic content has finished updating. There is no universal readiness condition for every application. Prefer waiting on the page’s own state over inserting an arbitrary fixed delay; capture only after the desired background and content have rendered.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Capture a specific element in Python

When the output should be one element rather than the whole current window, locate the target and use that element’s screenshot method. This saves a PNG of the element in bindings that expose the documented element screenshot API. For example:

from selenium import webdriver
from selenium.webdriver.common.by import By

with webdriver.Firefox() as driver:
    driver.set_window_size(1440, 1000)
    driver.get("https://example.com")

    card = driver.find_element(By.CSS_SELECTOR, ".product-card")
    saved = card.screenshot("./product-card.png")
    if not saved:
        raise OSError("Element screenshot could not be saved")

Use a selector that identifies the intended component, and wait for it to be ready before capturing. If the component’s background comes from a parent, the element-only image may not include that parent’s styling. Inspect the saved PNG and adjust the target or page structure if the result is incomplete.

Set viewport dimensions for repeatable layout

Viewport size affects responsive breakpoints, line wrapping, image dimensions, and which content is visible. The Selenium project documentation says, “Screen resolution can impact how your web application renders, so WebDriver provides mechanisms for moving and resizing the browser window.” Set a deliberate window size when layout consistency matters, as the Python example does with set_window_size(1440, 1000).

For repeatable screenshots, keep the browser and driver versions consistent in the target environment and wait for the page’s intended state. These are practical test controls, not guarantees that every browser will render identically. If the goal is an exact viewport rather than a particular outer window size, verify the browser’s actual viewport dimensions in your test environment.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Other output and full-page options

Selenium examples also show screenshots represented as Base64-encoded PNG data, which can be useful when a test needs to process or transmit image data instead of saving it directly. The binding determines the exact method and return format; check its API documentation and handle the result as PNG data rather than assuming every binding writes a file in the same way.

For Firefox with Python, the official Firefox WebDriver API lists full-document screenshot methods. Consult the Firefox WebDriver API reference for those methods and their current behavior. Do not treat them as universal across browser and binding combinations.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you need a screenshot from a URL without configuring Selenium and a browser driver, ScreenshotNeo offers a website screenshot API and MCP server. One GET request can return a PNG, JPEG, WebP, or PDF; its clean-shot workflow accepts cookie/consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture. Each of those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server exposes take_screenshot, get_page_info, and capture_pdf for AI agents and other MCP clients.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

See the ScreenshotNeo API documentation for request options and response behavior. The service has 1,000 screenshots per month on its free plan with no card required; paid plans start at $5 for 3,000 screenshots. Sign up for 1,000 free screenshots a month, with no card required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshooting

The background is missing

  • Check which element owns the background. A wrapper or component may paint it instead of body.
  • Confirm that the CSS change happens before capture and that the page has reached the intended state.
  • For a background image, verify the CSS URL and that the image has loaded. A color-only property will not set an image or gradient.
  • Open the saved PNG to distinguish a capture problem from a page-styling problem.

The screenshot has the wrong layout or crop

  • Set the browser window size before navigation or capture and confirm responsive breakpoints at that size.
  • For an element screenshot, verify the selector and check whether the background belongs to a parent outside the captured element.
  • If you need content beyond the current viewport, use a full-document method only when the chosen browser and binding document support for it.

The file is not saved

  • For Python save_screenshot, use a writable location and a filename ending in .png; inspect the Boolean return value.
  • Check that the process has permission to write the destination and that the expected directory exists.
  • For other bindings, confirm whether the screenshot method returns bytes, Base64 text, or a success value, and save or decode it accordingly.

The capture is blank or stale

  • Wait for the application-specific content or element you need instead of relying on a fixed delay.
  • Check whether the desired page state was changed by JavaScript and whether that change is permitted in the test.
  • Keep browser and driver versions consistent when comparing captures across runs.

Frequently Asked Questions

Does Selenium make screenshots transparent by default?

No transparency guarantee is documented for the screenshot methods discussed here. For a dependable visible background, style the page before capture and inspect the output PNG.

Can Selenium capture a full page in every browser?

No universal support is established. The Python Firefox API documents full-document screenshot methods; check the current API for your specific browser and binding.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.