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

In Selenium Python, locate the element, scroll it into view, then call element.screenshot("target.png") to save a PNG crop of that element. If you need the surrounding page instead, take a driver-level screenshot after scrolling.

Capture an element after scrolling it into view

Use a JavaScript call to control the scroll alignment, then take the screenshot from the same Selenium WebElement:

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

# Start the browser and navigate to the page under test.
driver = webdriver.Chrome()
driver.get("https://example.com")

try:
    element = driver.find_element(By.CSS_SELECTOR, "#target")
    driver.execute_script(
        "arguments[0].scrollIntoView({block: 'center', inline: 'nearest', behavior: 'instant'});",
        element,
    )
    element.screenshot("target.png")
finally:
    driver.quit()

Replace #target with a locator that matches the element on your page. The complete example uses Selenium’s Python API and Chrome WebDriver; the browser driver must be available and configured for your environment. Selenium documents WebElement.screenshot(filename) as saving the current element to a PNG file, so use a .png filename.

For the screenshot bytes rather than a file, use element.screenshot_as_png; for base64-encoded image data, use element.screenshot_as_base64. See the Selenium Python WebElement API.

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

Choose element or viewport capture

Capture method What it captures Use it when
element.screenshot("target.png") The located element You want a focused crop without surrounding page content.
driver.save_screenshot("page.png") or driver.get_screenshot_as_file("page.png") The visible browser viewport You want the page context around the element after scrolling.

A normal driver-level screenshot is a viewport capture; these APIs do not establish that a basic screenshot automatically includes the entire long document. Selenium’s WebDriver documentation and Python API reference cover browser and element capture methods.

Set scroll alignment for the page layout

scrollIntoView() scrolls ancestor containers to make the element visible. Its options let you choose vertical and horizontal alignment and whether the movement is animated. The simplest Selenium form is:

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
driver.execute_script("arguments[0].scrollIntoView(true);", element)

true aligns the element’s top edge with the visible area of its scrollable ancestor, while false aligns its bottom edge. The equivalent option objects are {block: "start", inline: "nearest"} and {block: "end", inline: "nearest"}. See MDN’s Element.scrollIntoView() reference.

Keep a fixed header from covering the element

If a sticky or fixed header overlaps an element aligned at the top, center it instead:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
driver.execute_script(
    "arguments[0].scrollIntoView({block: 'center', inline: 'nearest', behavior: 'instant'});",
    element,
)

Alternatively, the page can provide a suitable CSS scroll-margin-top value for the target. MDN notes scroll margins as a way to account for fixed headers. The actual composition depends on the page’s CSS and scroll containers, so check the result in the browser environment used for the test.

Choose instant, automatic, or smooth movement

  • behavior: "instant" requests a single jump, useful when the next operation is an immediate capture.
  • behavior: "smooth" animates the scroll over time; do not assume the page has settled as soon as the script returns.
  • behavior: "auto" follows the computed CSS scroll-behavior.
  • block: "nearest" attempts to move the least amount and does not scroll if the element is already visible.

MDN documents the scroll behavior and alignment options. For animated scrolling or pages that rerender during scrolling, wait for an application-specific condition that confirms the relevant state before capturing; a fixed sleep is not reliable across pages.

Handle dynamic pages and stale elements

Selenium checks that a referenced element is still attached to the DOM when an element operation runs. If the page replaces that node during a rerender, Selenium raises StaleElementReferenceException; later operations on the old reference will continue to fail. Wait for the page’s relevant state, then locate the element again before scrolling and capturing. The behavior is described in the WebElement API reference.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

For pages that load images or other content lazily, scrolling may trigger a content update. Wait for the specific image or application state you need rather than assuming that the scroll alone guarantees all content has loaded.

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

Troubleshoot common capture problems

  • No such element: The locator did not match an element at the time of lookup. Check the selector and wait until the target is present before locating it.
  • Stale element reference: The page detached or replaced the element after you found it. Wait for the rerender to finish, reacquire the element, then scroll and capture.
  • Target is hidden by a header: Top alignment placed it behind a fixed or sticky header. Use center alignment or a page-appropriate scroll-margin-top.
  • Screenshot appears before animation or content settles: Smooth scrolling, lazy loading, or page updates are still in progress. Request instant scrolling where appropriate and wait for a meaningful application condition.
  • Screenshot is cropped too tightly: Element capture intentionally focuses on the element. Use a driver-level screenshot if you need visible surrounding context.
  • Screenshot does not show the whole page: A driver-level screenshot captures the viewport, not a guaranteed full-document image. The cited basic capture APIs do not establish universal full-page behavior.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

For a URL-based capture without configuring Selenium and a browser driver, ScreenshotNeo provides a screenshot API and an MCP server for AI agents. Cookie banners are accepted and removed along with 60+ known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers report the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients.

One GET request returns an image or PDF. For a PNG capture of a URL, see the ScreenshotNeo API documentation for response-format options:

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

The free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000. Learn more at ScreenshotNeo or sign up free.

Frequently Asked Questions

Can I save a Selenium element screenshot as JPEG?

The documented Python element screenshot methods save PNG files or return PNG data; the file method expects a .png path.

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

Does `scrollIntoView()` scroll nested containers too?

By default, it scrolls scrollable ancestor containers as needed to bring the element into view.

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.