October 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 NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Return a Selenium Screenshot as a Base64 String

Use Selenium’s Python get_screenshot_as_base64() method to return a screenshot as Base64 text, then add a PNG data URL prefix only when embedding it in HTML.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

In Python, call driver.get_screenshot_as_base64() on an active Selenium WebDriver session. It returns a Base64-encoded screenshot string—not PNG bytes and not an HTML data URL. Add the data:image/png;base64, prefix only if the next step needs an image source for HTML.

Return the screenshot as Base64 text in Python

Navigate to the page, call Selenium’s Base64 screenshot method, then pass the returned string to the code that needs it. This example uses Chrome:

from selenium import webdriver

 driver = webdriver.Chrome()
 try:
     driver.get("https://example.com")
     screenshot_b64 = driver.get_screenshot_as_base64()
     # screenshot_b64 is a Base64-encoded string
 finally:
     driver.quit()

Remove the extra leading spaces before driver in the code above if copying it as a top-level Python script; the runnable version is:

from selenium import webdriver

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    screenshot_b64 = driver.get_screenshot_as_base64()
    # screenshot_b64 is a Base64-encoded string
finally:
    driver.quit()

The method captures the current window screenshot. Selenium’s Python API documents its return type as str. See the Python WebDriver API.

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

Choose the output format your next step needs

Needed output Python method What you receive Use it for
Base64 text driver.get_screenshot_as_base64() A Base64-encoded str Transporting or embedding the encoded screenshot
PNG bytes driver.get_screenshot_as_png() Decoded PNG bytes In-memory image processing or writing bytes yourself
PNG file driver.save_screenshot("screenshot.png") Saves a PNG and returns a Boolean success value Saving directly to a path
Element screenshot Use the relevant element screenshot API for your binding Screenshot for the selected element, where supported Capturing one element instead of the driver’s current window

Selenium’s WebDriver screenshot response is Base64-encoded. Don’t decode the result if the consumer expects Base64 text. Decode only when the next operation expects raw image bytes. API names and return types vary across language bindings; check the relevant binding’s documentation.

Embed the Base64 string in HTML

An HTML image source needs a data URL, which consists of the image MIME type prefix followed by the Base64 payload:

img_src = f"data:image/png;base64,{screenshot_b64}"

Use that value as an image’s src. The prefix identifies the content as a PNG data URL; the Base64 string by itself is not a complete data URL.

Capture scope: current window or element

The Python driver method is documented as a screenshot of the current window. Do not assume it captures an arbitrarily long, full-page document across all browsers and implementations. If you need a particular element, use the element-level screenshot API documented for your binding. Selenium’s Java reference describes screenshot scope and best-effort behavior for nonconformant implementations; verify the result in your target browser and binding.

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

Other Selenium language bindings

Binding Documented call Result
Python driver.get_screenshot_as_base64() Base64 str
Java ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64) Base64 string
JavaScript await driver.takeScreenshot() Encoded string

Selenium’s official documentation shows the JavaScript screenshot method and writing the result using a Base64 encoding argument. The Java API reference describes TakesScreenshot and OutputType.BASE64. Consult the API reference for the specific language and version you use.

Troubleshoot common problems

  • You got a string, but the image consumer rejects it: Check whether it expects Base64 text, raw PNG bytes, or a data URL. Add data:image/png;base64, only for an HTML image source.
  • You decoded the text and now need Base64: Keep the original value from get_screenshot_as_base64(); use get_screenshot_as_png() only when raw PNG bytes are needed.
  • The result shows only the visible window, not the whole document: The driver method is documented for the current window. Use an appropriate full-page approach for your browser and verify its behavior; don’t assume the standard call captures an arbitrarily long page.
  • You need only one component of the page: Use the element screenshot API where available rather than treating the driver screenshot as an element capture.
  • The browser session is unavailable: The call requires an active WebDriver session. Ensure navigation and screenshot capture happen before calling driver.quit().

Or skip the browser setup

ScreenshotNeo returns a screenshot or PDF from one GET request, without setting up a Selenium browser session. Its clean-shot steps can accept cookie banners and remove more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers identify the page verdict and billing status. An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents.

For example, this cURL request saves a WebP screenshot:

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 documentation for request options and response details. It includes 1,000 screenshots per month free with no card; paid plans start at $5 for 3,000. Sign up for the free plan.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Does get_screenshot_as_base64() return a data URL?

No. It returns Base64 text. To use it as an HTML image source, prefix it with data:image/png;base64,.

Do I need to decode the result before sending it to an API?

Only if that API expects raw PNG bytes. If it expects a Base64 string, send the returned string as-is.

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. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. 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…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

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.