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.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallChoose 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.
#1 Best Overall
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 toscrollIntoView(true).block: "end"aligns its bottom edge; this corresponds toscrollIntoView(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.smoothanimates the scroll, whileautofollows 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.
Rank #2
- 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.
Rank #3
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-topfor 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.
StaleElementReferenceExceptionoccurs: 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:
Recommended Free Tools
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
- 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
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.
Quick Recap
Best Value
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.




