DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk5 min

HTML to Image Conversion in Python with Selenium

Use Selenium WebDriver to open local or remote HTML in a browser and save its rendered window as a PNG, with guidance for viewport sizing, full-page capture, and dynamic content.

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.

To convert HTML into an image with Python and Selenium, open the HTML in a browser controlled by WebDriver and save a screenshot. For a local page, navigate to its file URL; for a hosted page, navigate to its web URL. Selenium’s standard screenshot methods capture the current browser window as a PNG.

Convert a local HTML file to a PNG

Install Selenium, save your markup as page.html, then run this script from a Python environment with a supported browser available:

from pathlib import Path
from selenium import webdriver

html_path = Path("page.html").resolve()
output_path = Path("output.png").resolve()

driver = webdriver.Chrome()
try:
    driver.get(html_path.as_uri())
    saved = driver.save_screenshot(str(output_path))
    if not saved:
        raise OSError(f"Could not save screenshot: {output_path}")
finally:
    driver.quit()

Path.resolve() makes the paths absolute, and as_uri() produces a local file: URL for navigation. The output is written to output.png in the current working directory. This captures the current browser window, not necessarily the entire document.

Install and start Selenium

  1. Install the current Selenium Python package with python -m pip install -U selenium. The Selenium Python documentation currently lists Python 3.10 or newer as supported; check the official Selenium documentation for current requirements and browser support.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  2. Install or make available a supported browser such as Chrome, Edge, Firefox, or Safari. Selenium requires a browser driver. In modern supported setups, Selenium Manager can handle browser-driver installation and configuration when you instantiate WebDriver; manual setup may still be needed in constrained environments.

  3. Run the script in an environment where the browser can start. A local Selenium script does not need the Java server; remote WebDriver execution requires Selenium Grid.

For code that writes screenshots to memory instead of a file, Selenium’s Python WebDriver API also provides get_screenshot_as_png(). The API documents save_screenshot(filename) and get_screenshot_as_file(filename) for file output, and base64 methods for embedding. See the Python WebDriver API reference.

Capture a remote page or set a precise viewport

For a remote page, navigate to its URL rather than a local file URI. If the dimensions matter, explicitly size the browser window before capture:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
driver.set_window_size(1280, 900)
driver.get("https://example.com")
driver.save_screenshot("output.png")

Set the size before capture (and, where practical, before navigation) to make the browser viewport predictable. Actual output can still depend on browser chrome, scrollbars, page zoom, device scale, fonts, and when images or scripts finish loading. Verify dimensions and rendering in the same browser and runtime you intend to use.

Choose viewport, element, or full-page capture

Current window

save_screenshot() captures the current browser window. Use it when the image should show the visible viewport at the current scroll position.

One element

When the goal is a component—such as a card or chart—rather than the viewport, locate the element and use Selenium’s element screenshot method. Selenium documents element screenshot capture in its WebDriver documentation. This avoids cropping a larger page image by hand, though the element must be present and visible to capture.

Entire document

Full-document capture is not a universal cross-driver promise. The Firefox Python API documents driver.get_full_page_screenshot_as_file(path) and driver.save_full_page_screenshot(path). Check the API for your selected browser driver and test the result against your target browser version; do not assume those Firefox-specific methods work with Chrome or every WebDriver implementation. See the Python API reference.

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

Wait for dynamic content before taking the screenshot

A screenshot records the browser’s rendered state at capture time. If JavaScript is still populating the page, fonts have not loaded, or images are still appearing, the saved image may be incomplete. Prefer waiting for a meaningful page element or an application-ready condition over relying only on an arbitrary fixed delay. Then take the screenshot. Selenium’s screenshot methods do not themselves guarantee that a site’s asynchronous content is ready.

For stable visual output, also check that required assets are accessible from the browser, that the page has the expected viewport and scroll position, and that any lazy-loaded content has actually been triggered. A full-page method and a viewport method may behave differently with content loaded only after scrolling.

Check the result and close the browser reliably

The file-saving screenshot methods return a boolean. Check it and raise a clear error if saving fails, as the example does. Use an absolute output path when the script may be launched from different working directories. Put driver.quit() in a finally block so the browser session is closed even when navigation or capture raises an exception.

Troubleshooting

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 want an API call instead of managing WebDriver, ScreenshotNeo returns a screenshot or PDF from one GET request. For example, this saves a WebP capture of a page:

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

See the ScreenshotNeo documentation for API details. ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are not billed. It also offers an MCP server for AI agents, and includes 1,000 screenshots per month free with no card; paid plans start at $5 for 3,000. Sign up for the free plan.

When Playwright may fit better

If your project is not committed to Selenium, Playwright is another documented Python option. Its library guide covers Chromium, Firefox, and WebKit, and its screenshot guide uses page.screenshot(path="screenshot.png"); browsers run headless by default. The documentation establishes these capabilities, not a general performance or image-fidelity advantage over Selenium. Choose based on the browser behavior, capture scope, and automation library your project needs. See the Playwright library guide and screenshot guide.

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

Frequently Asked Questions

Can Selenium save a screenshot as JPEG or WebP?

The Selenium file-saving methods described here save PNG screenshots. The ScreenshotNeo example in this article returns WebP by default.

Does taking a Selenium screenshot require Java?

Not for a local Selenium script. Remote WebDriver execution uses Selenium Grid.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.