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 Capture a Selenium Screenshot After Scrolling to an Element

Use Selenium Python to scroll a located element into view and save its screenshot, with guidance on viewport captures, sticky headers, dynamic pages, and stale elements.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Find the target 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

This Selenium Python example centers the target in its scrollable ancestors, requests an immediate rather than animated scroll, and saves the element itself as a PNG. Replace #target with a locator for the element on your page.

As an Amazon Associate I earn from qualifying purchases.

from selenium.webdriver.common.by import By

 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")

The example assumes that driver is an initialized Selenium WebDriver and that the page is already loaded. Selenium’s Python API documents WebElement.screenshot(filename) as saving the current element to a PNG; the path should end in .png. It also offers element.screenshot_as_png and element.screenshot_as_base64 when you need bytes or Base64 data instead of writing a file. See the Selenium Python WebElement API.

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

Choose whether to capture the element or the viewport

Method What it captures Best use
element.screenshot("target.png") The located element A focused crop without surrounding page content
driver.save_screenshot("page.png") The visible browser page/window The element plus surrounding context after scrolling

The driver-level method captures the visible area; it does not, by itself, establish that a long document will be captured as a full-page image. Selenium’s WebDriver documentation and Python API describe the element and driver screenshot methods.

Set scroll alignment to suit the page

scrollIntoView() scrolls ancestor containers to make an element visible. The options let you choose vertical alignment, horizontal alignment, and scroll behavior. For example, the short form below aligns the target at the top:

driver.execute_script("arguments[0].scrollIntoView(true);", element)

Top alignment can put the element underneath a fixed or sticky header. Center alignment, as in the main example, often avoids that overlap. For a page whose layout you control, CSS scroll-margin-top can also reserve space above the target when it is scrolled to the top. MDN documents these options and the role of scroll margins in its scrollIntoView reference.

  • block: "start" aligns the element’s top edge; this corresponds to scrollIntoView(true).
  • block: "end" aligns its bottom edge; this corresponds to scrollIntoView(false).
  • block: "center" places it near the middle of the visible area.
  • block: "nearest" attempts the smallest movement and does not scroll if the element is already visible.
  • inline: "nearest" limits unnecessary horizontal movement.
  • behavior: "instant" requests a single jump. smooth animates the scroll, while auto follows the computed CSS scroll behavior.

The final composition still depends on nested scroll containers and the page layout. Check the result in the browser and driver configuration used for your automation; identical rendering across setups is not guaranteed by these API descriptions.

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.
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

Wait when the page moves or rerenders

With instant scrolling, the capture can usually follow the scroll call directly. If the page uses animated scrolling, loads content lazily, or updates the target as it moves, wait for an application-specific condition that indicates the content is ready. A fixed sleep is not universally reliable because animation, rendering, and network timing vary.

A located WebElement can also become stale if the page detaches it during a rerender. Selenium checks element freshness during operations; after detachment, calls on that old reference raise StaleElementReferenceException. Wait for the updated state and locate the element again before scrolling and capturing. See the WebElement API.

Common problems and fixes

  • The screenshot misses the target or shows the wrong region: Confirm the locator resolves to the intended element, then use an explicit alignment such as block: "center". For viewport context, use the driver screenshot method rather than the element method.
  • A fixed header covers the target: Prefer center alignment or set a suitable scroll-margin-top for the target in the page’s CSS.
  • The target moves during capture: Avoid smooth scrolling for an immediate capture, or wait for the relevant animation or application update to finish.
  • StaleElementReferenceException occurs: The element was detached or replaced. Wait for the rerender to finish and find the element again instead of reusing the stale reference.
  • The saved element image is not the whole page: Element screenshots crop to the element, while driver screenshots cover the visible area. The cited basic methods do not establish universal full-page capture.

Or skip the browser setup

If you need a screenshot from a URL without setting up Selenium and a browser, ScreenshotNeo offers a one-request screenshot API. Its clean-shot flow accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents and other MCP clients.

Example cURL request, with the target URL set to Stripe:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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 API documentation for request parameters. ScreenshotNeo supports PNG, JPEG, WebP, or PDF output. The free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 shots. Learn about ScreenshotNeo or sign up for 1,000 free screenshots a month with no card.

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
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Can Selenium return an element screenshot without saving a file?

Yes. Use element.screenshot_as_png for PNG bytes or element.screenshot_as_base64 for Base64 data.

Does scrollIntoView() always scroll the whole page?

It scrolls the element’s scrollable ancestors as needed; nested containers and page layout affect which areas move.

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
Windows Errors? Fix Them Before They SpreadFree repair scan
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.