October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk7 min

How to Take a Screenshot of a Cross-Origin iframe with Selenium

Use Selenium’s iframe element screenshot for a rendered frame capture, switch context only to inspect inner elements, and choose a separate strategy for precise crops or full-document output.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To capture a cross-origin iframe with Selenium, locate the iframe in the parent page and call your Selenium binding’s element screenshot method on it. WebDriver’s documented behavior treats an iframe element screenshot as a capture of that frame’s body region. Cross-origin status does not, by itself, prevent WebDriver from switching into the child browsing context; it does not grant ordinary page JavaScript permission to read the iframe’s DOM.

Choose the capture you actually need

“Screenshot the iframe” can mean several different things. Pick the target before choosing an API, because an element capture, a crop of the parent viewport, and a full capture of the embedded document are not interchangeable.

As an Amazon Associate I earn from qualifying purchases.

Goal Approach Key limitation
Capture the rendered iframe region Selenium element screenshot on the iframe element Confirm that the browser and driver capture the BODY region and crop you expect. The explicit behavior appears in a 2015 W3C WebDriver working draft, not a current final specification. W3C WebDriver working draft
Capture exactly how the iframe appears in the parent viewport Capture the top-level page and crop to the iframe’s visible rectangle You must account for viewport coordinates, scrolling, scaling, borders and clipping.
Capture a precise region with Chromium Use Chrome DevTools Protocol (CDP) Page.captureScreenshot with its clip parameter This is Chromium-oriented and version-sensitive, not a portable WebDriver command. Chrome DevTools Protocol
Capture the entire embedded document Use a full-document strategy appropriate to the browser, or scroll and capture segments An iframe element screenshot is not automatically a full-page capture of a taller inner document.

Selenium’s official documentation describes WebDriver as browser automation and documents screenshot methods in its language bindings. The method names and protocol details vary by binding. Selenium documentation

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

Capture the iframe element with Selenium in Python

This example waits for the iframe element, then saves its element screenshot as a PNG. Replace the example URL and iframe locator with those used by your page. It assumes Selenium is installed and that a compatible browser and driver are available to your Selenium setup.

#1 Best Overall
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

url = "https://example.com/page-with-iframe"
iframe_locator = (By.CSS_SELECTOR, "iframe#report")

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)

try:
    driver.get(url)
    iframe = WebDriverWait(driver, 20).until(
        EC.presence_of_element_located(iframe_locator)
    )
    WebDriverWait(driver, 20).until(EC.visibility_of(iframe))

    if not iframe.screenshot("iframe.png"):
        raise RuntimeError("Selenium could not save the iframe screenshot")
finally:
    driver.quit()

The WebDriver draft says an element screenshot of a FRAME or IFRAME corresponds to the frame’s BODY. Validate the result in your browser and driver combination: the draft dates to 2015, and it does not guarantee that every implementation will match every requested crop. Selenium’s Python API also documents current-window screenshot methods such as save_screenshot, get_screenshot_as_png and get_screenshot_as_base64; those capture the current window, not necessarily just the iframe. Python WebElement API · Python WebDriver API

Switch into a cross-origin iframe only to inspect or interact

You do not need to switch into the iframe just to take the element screenshot above. Switch when your automation needs to find or operate on elements inside the child document. The cited W3C WebDriver draft explicitly says WebDriver is not bound by the same-origin policy when switching into child browsing contexts. That is a statement about WebDriver automation, not permission for JavaScript running in the parent page to traverse a cross-origin frame’s DOM. W3C WebDriver working draft

iframe = WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "iframe#report"))
)
driver.switch_to.frame(iframe)

try:
    # Locate or interact with elements inside the frame here.
    heading = WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "h1"))
    )
    print(heading.text)
finally:
    driver.switch_to.default_content()

Switching changes the context for subsequent element lookups. Return to the top-level document with default_content() before locating parent-page elements. If you only need pixels, the element screenshot is the simpler first attempt.

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

When you need a parent-page crop or a full inner document

Capture the iframe as it appears in the parent viewport

If the element screenshot does not produce the visible rectangle you need, capture the top-level page and crop using the iframe’s geometry. Ensure the rectangle is expressed in the coordinate system expected by your screenshot mechanism. Scrolling, device scale, borders and partial clipping can all shift or reduce the visible region; test those conditions rather than assuming the element’s location alone defines the final crop.

Rank #2
Dell Latitude 5420 14" FHD Business Laptop Computer, Intel Quad-Core i5-1145G7, 16GB DDR4 RAM, 256GB SSD, Camera, HDMI, Windows 11 Pro (Renewed)
  • 256 GB SSD of storage.
  • Multitasking is easy with 16GB of RAM
  • Equipped with a blazing fast Core i5 2.00 GHz processor.

Use Chromium CDP for a protocol-level clip

Chromium’s CDP Page.captureScreenshot defines a clip parameter, along with image-format and beyond-viewport options. It can be useful for precise region captures, but it is browser-specific and its invocation depends on your Selenium binding and browser version. The protocol documentation does not make it a portable WebDriver method. Chrome DevTools Protocol

Do not confuse the iframe viewport with the embedded page’s full height

The iframe element has a visible rectangle in the parent page. Its inner document can be taller than that rectangle. A screenshot of the iframe region should not be described as a full-page screenshot of the embedded site unless you have verified a separate full-document method. Depending on the requirement, you may need to scroll within the frame and capture segments or use a browser-specific full-page strategy.

Wait for the content you want to photograph

Finding a visible iframe does not prove its embedded content has finished rendering. Lazy loading, navigation, consent screens or delayed content can leave the capture blank or incomplete. A practical sequence is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Wait for the iframe element to be present and visible.
  2. If content readiness matters, switch into the frame and wait for a known inner element or other condition specific to the page.
  3. Return to the top-level document if needed, then capture the iframe element.
  4. Inspect the saved image for clipping, blank areas and the expected content before relying on it in a test or workflow.

There is no universal inner selector or readiness signal: choose one that reflects the content your page requires, and handle consent screens or authentication according to your test setup.

Rank #3
15.6 Inch Win 11 Laptop Computer, N4020, 4GB DDR4 RAM, 128GB Storage | LPDDR4 RAM, 180° Lay-Flat, Dual-Band WiFi, Type-C PD Fast Charging, Thin & Lightweight for Students, Home & Office, Gray
  • WINDOWS 11 | STABLE PERFORMANCE: Powered by Intel Celeron N4020 processor and Windows 11 system, this laptop delivers stable performance for everyday computing tasks. It supports web browsing, online learning, document editing, email communication, and basic office work with optimized power efficiency, providing a practical and reliable experience for essential daily use for daily use.
  • 15.6” FHD IPS DISPLAY: Features a 15.6-inch Full HD IPS display with narrow bezels, offering wider viewing angles and clearer image details compared to standard panels. The improved screen-to-body ratio enhances visual experience for study, reading, document work, and video playback, making it suitable for both productivity and entertainment use.
  • 4GB DDR4 + 128GB eMMC STORAGE: Equipped with 4GB DDR4 memory and 128GB eMMC storage for everyday basics such as browsing, documents, email, and online learning platforms. The built-in TF card slot supports storage expansion up to 1TB, giving you more flexibility for files, photos, videos, and daily documents. TF card not included.
  • CONNECTIVITY & PORTS: Includes 1× TF card slot, 2× USB 3.2 Gen1 ports, and 2× full-featured Type-C ports (USB 3.2 Gen1). The Type-C ports support data transfer, charging, and video output, enabling flexible connection with external devices such as monitors, storage, and peripherals for daily work and study use.
  • LIGHTWEIGHT DESIGN | ONLINE COMMUNICATION: Designed with a slim, portable profile, this laptop is easy to carry for school, commuting, and travel. A built-in 1MP front camera supports online classes, video meetings, remote communication, and everyday conferencing. The 3300mAh battery works with the low-power system design to support practical daily use, while thermal optimization helps maintain quieter operation during extended tasks.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting

  • The locator cannot find the iframe: Check the selector and ensure you are searching from the top-level page, not a different frame context. If the iframe is inserted later, wait for it to appear.
  • The screenshot is blank or incomplete: The frame may still be navigating or rendering. Wait for a meaningful inner element, then check for lazy-loaded content, consent walls or other delayed states.
  • The element screenshot has the wrong dimensions or crop: Confirm whether you need the iframe BODY region, the parent viewport’s visible rectangle or the entire embedded document. Test the actual browser and driver; the explicit iframe screenshot behavior cited here is from a 2015 working draft.
  • Inner elements are not found: Switch to the iframe before looking them up, and return to the parent context with driver.switch_to.default_content() when finished.
  • Parent JavaScript gets a cross-origin access error: Do not use page-script DOM access as a substitute for WebDriver context switching. The WebDriver draft’s cross-origin statement applies to automation, not ordinary same-origin policy rules for page JavaScript.
  • CDP commands fail or behave differently: Check the Chromium browser version and the binding-specific CDP invocation. CDP is not the portable WebDriver screenshot API.

Portability and protocol direction

Selenium describes WebDriver BiDi as a W3C bidirectional protocol and a cross-browser direction for browser automation, while its documentation also describes functionality currently available through CDP. The reviewed Selenium documentation does not establish BiDi screenshot-capture parity with CDP’s clipped screenshot command, so do not assume an equivalent exists for your setup. Selenium WebDriver BiDi

Or skip the browser setup

For a one-call screenshot API, ScreenshotNeo accepts a URL and returns an image or PDF. Example using cURL:

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

See the ScreenshotNeo API documentation for request options. ScreenshotNeo removes cookie and consent banners, newsletter popups and chat widgets before the shot; those steps can be turned off. Bot checks, blank pages, timeouts, failed loads and cache hits are not billed, and the response identifies the page verdict and billing status in headers. Its MCP server provides take_screenshot, get_page_info and capture_pdf tools for AI agents. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. For a page containing an iframe, verify that the returned capture includes the content and region you need.

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

Sign up for 1,000 free screenshots a month, with no card.

Frequently Asked Questions

Does an iframe screenshot reveal the iframe’s HTML?

No. It captures rendered pixels; it does not provide the parent page script with access to a cross-origin document’s DOM.

Is Selenium’s iframe element screenshot behavior guaranteed by a current final W3C specification?

The explicit iframe-to-BODY wording cited in this guide is in a 2015 W3C WebDriver working draft, so validate the result with your browser, driver and Selenium 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.

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

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. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair 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.