WebElement#getScreenShotAs(OutputType.File) not working WebElement#getScreenShotAs(OutputType.File) not working selenium selenium

WebElement#getScreenShotAs(OutputType.File) not working


The real reason for the error is that many / most WebDriver implementations do not actually support element-based screenshots, despite WebElement extending TakesScreenshot since 2.47.0. Perhaps someday this will change.

If you want screenshots you need to do them at the whole-browser level, in which case - as other answers have it - you need to pass the WebDriver instance.

File ssFile = ((TakesScreenshot)(driver)).getScreenshotAs(OutputType.FILE);

Strictly speaking you should probably do the following, since not all Drivers are guaranteed to support screenshots, e.g. HtmlUnitDriver.

if (!(getDriver() instanceof TakesScreenshot)) {    File ssFile = ((TakesScreenshot)(driver)).getScreenshotAs(OutputType.FILE);    // ...}

There are alternate solutions for single-element screenshots, but they inevitably involve cropping of the full-browser screenshot. See, for example: https://stackoverflow.com/a/13834607/954442

Update: just to clarify that this is not a bug, it's that although element screenshots are a part of the W3C WebDriver spec, different browsers have different levels of compliance/coverage, and as far as I know this feature is only supported by Microsoft Edge.


Dont Import these lib from Suggestions,

import org.eclipse.jetty.server.Response.OutputType;

import org.seleniumhq.jetty9.server.Response.OutputType;

Import these lib.

import org.openqa.selenium.OutputType;


It should be something like this.

File screenS = ((TakesScreenshot)(driver)).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(screenS, new File("C:\\akshay\\del\\screenshots\\1.jpg"));

replace the above code with

element.getScreenshotAs(OutputType.FILE);        File destination=new File("Image.png");        FileUtils.copyFile(null, destination);