Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.
If element.getScreenshotAs(OutputType.FILE) fails, first work out whether the failure happens during capture or when you save the returned file. In Selenium’s Java API, a WebElement can take a screenshot when its concrete driver implementation supports the operation. The returned File is temporary: copy it to a destination you control before the JVM exits.
Use the element screenshot API, then copy its temporary file
For a single element, the basic pattern is to find the element in the active WebDriver session, capture it as a file, and copy that file to a persistent path:
WebElement element = driver.findElement(By.cssSelector("h1"));
File temporaryScreenshot = element.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(temporaryScreenshot, new File("./element.png"));
The capture and save are separate operations. If getScreenshotAs returns, Selenium produced a temporary file; a later exception during the copy is a file destination or write problem, not evidence that element screenshot capture is unsupported. Selenium’s Java API documents WebElement as extending TakesScreenshot, but the concrete driver or remote implementation must still support the operation.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Complete Java example using Apache Commons IO
This example uses the Commons IO FileUtils utility used in Selenium’s documented Java example. Add Apache Commons IO to the project if it is not already a dependency.
#1 Best Overall
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ElementScreenshot {
public static void main(String[] args) throws IOException {
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com");
WebElement element = driver.findElement(By.cssSelector("h1"));
File temporaryScreenshot = element.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(temporaryScreenshot, new File("./element.png"));
} finally {
driver.quit();
}
}
}
The example’s destination is relative to the process working directory, which may not be the project directory in an IDE, build tool, or CI runner. Use an absolute path or log the resolved destination if you are unsure where the file went. The parent directory must exist and be writable.
Save with Java file APIs instead
If you do not want an Apache Commons IO dependency, use Java’s NIO file-copy API. Create the destination’s parent directory when necessary, then copy the temporary file:
Rank #2
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
// Assume element is already located in the active WebDriver session.
File temporaryScreenshot = element.getScreenshotAs(OutputType.FILE);
Path destination = Path.of("./element.png");
Path parent = destination.toAbsolutePath().getParent();
if (parent != null) {
Files.createDirectories(parent);
}
Files.copy(temporaryScreenshot.toPath(), destination,
StandardCopyOption.REPLACE_EXISTING);
Choose a destination filename and extension that match your intended use. The API target FILE does not let you keep Selenium’s temporary file simply by renaming the variable; the explicit copy is what puts the image at your chosen persistent path.
Free tools Windows power users keep installed
One-click scans. No signup required.
Check the API types and the point of failure
- Confirm the receiver is a WebElement. Call the method on the element returned by the active session, not on a locator such as
By.cssSelector(...)or a variable holding the driver. - Use Selenium’s output target. The argument should be Selenium’s
OutputType.FILE, not a similarly named type from another library. The generic method returns a value corresponding to the selected output type. - Split capture from persistence. Keep the call and copy on separate lines. This makes it clear whether an exception comes from Selenium taking the screenshot or from writing the file to your destination.
- Check the actual exception and its line. Selenium documents
UnsupportedOperationExceptionwhen the implementation does not support capture andWebDriverExceptionwhen capture fails. A file-copy exception should be diagnosed as a filesystem issue instead.
Do not assume support for every browser, driver, Selenium release, or remote/Grid setup from the generic API declaration alone. The available API reference does not establish a universal version matrix. When support is in doubt, identify the concrete browser driver or remote implementation and the Selenium, browser, driver, and Grid versions actually in use.
Rank #3
Troubleshoot the common failures
| Symptom | Likely stage | What to check or do |
|---|---|---|
UnsupportedOperationException at getScreenshotAs |
Capture capability | Check whether the concrete browser driver or remote/Grid implementation supports element screenshots. The generic API does not promise support for every implementation/version combination. |
WebDriverException at getScreenshotAs |
Capture failure | Read the full exception and underlying message, then check the active session and concrete driver implementation. The exception type alone does not identify a single cause. |
StaleElementReferenceException |
Element reference | A navigation, refresh, or DOM update may have detached or replaced the node since it was found. Wait for the updated page to settle, locate the element again, and capture the new reference. |
Exception at copyFile or Files.copy |
Persistence | Confirm the destination parent exists, the test process can write there, and the path resolves where expected. Capture may already have succeeded if the temporary file was returned. |
| Code does not compile around the method call | API types or imports | Verify the variable is a Selenium WebElement, import Selenium’s OutputType, and pass OutputType.FILE. Check that the code is using Selenium’s Java API. |
| The result is the wrong scope | Screenshot choice | An element call captures an element. If you need the current browsing context rather than one element, use the driver-level screenshot call instead. |
Re-find an element after a page change
Do not keep and reuse an element reference across navigation or a DOM replacement. Perform the relevant page action, wait for the page or element state your test requires, and then call findElement again. Selenium checks element freshness; an old reference can be stale even if the page now displays a visually similar element.
Distinguish element capture from a page capture
element.getScreenshotAs(OutputType.FILE) is for the selected element. If your expected output is a screenshot of the current browsing context, use the driver-level TakesScreenshot call shown in Selenium’s Java usage example. Changing the receiver changes the scope; changing FILE to another output type does not turn an element screenshot into a page screenshot.
Choose FILE, BYTES, or BASE64 for the output
Selenium documents three useful output targets. Select based on what the next step in your code needs, rather than treating them as interchangeable filenames.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →| Output target | What you receive | When it fits |
|---|---|---|
OutputType.FILE |
A temporary file | Useful when your next step works with a file. Copy it promptly to a persistent destination. |
OutputType.BYTES |
Raw image bytes | Useful when code should process or write the image data itself instead of first handling a temporary file. |
OutputType.BASE64 |
Base64-encoded image data | Useful when the next step needs the image represented as text for transport or storage. |
With FILE, Selenium’s returned temporary file is not the same thing as your final named artifact. The temporary file is deleted when the JVM exits, so a test that needs to retain screenshots should copy the result before the process ends.
Best Value
Or skip the browser setup
If you need a screenshot of a public URL rather than one element in a live Selenium session, ScreenshotNeo offers a screenshot API. It is not a replacement for an element screenshot from the current WebDriver session: it captures a URL through its own service. For the Selenium task described above, use the DIY Java method; use the API when a URL-based capture is the right fit.
One cURL request returns the captured image:
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 API documentation for parameters. Python and Node.js examples are available if those are the languages in your capture workflow:
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://example.com"},
timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://example.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
- Before capture, it accepts the cookie or consent banner like a visitor and removes 60+ known consent platforms, newsletter popups, and chat widgets; each step can be turned off.
- Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing; the response identifies the page verdict and billing status in headers.
- An MCP server provides
take_screenshot,get_page_info, andcapture_pdftools for Claude, Cursor, and other MCP clients. - The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. Every feature is available on every plan.
Sign up for ScreenshotNeo’s free plan: 1,000 screenshots a month, no card required.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Practical reliability and cost notes
For a Selenium test, capture and copy while the relevant session and element are still usable; re-find the element after a page transition or DOM replacement. For an API-based capture, the destination is a URL rather than a live Selenium element, so it avoids setting up the browser session for that separate use case. ScreenshotNeo’s usage and response headers let you distinguish billed captures from the no-cost outcomes listed above. Do not use a URL capture service when the requirement is specifically to capture an element already present in an authenticated or otherwise stateful Selenium session.
Quick Recap
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.

