Mrunal Gosar
Mrunal Gosar

Reputation: 4683

WebElement#getScreenShotAs(OutputType.File) not working

I am trying to use WebElement#getScreenShotAs(OutputType.FILE) feature added in selenium webdriver 2.47.0 version on Firefox Browser Code

public static void main(String[] args) throws IOException {
        WebDriver driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://automationpractice.com/index.php");
        WebElement element=driver.findElement(By.cssSelector("a[title='Contact Us']"));
        System.out.println(element.getText());
        element.getScreenshotAs(OutputType.FILE);
        File destination=new File("Image.png");
        FileUtils.copyFile(null, destination);
    }

..But I am getting below exception:

Contact us
Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: Unrecognized command: GET /session/e796089b-1d64-4590-9157-a0716a57e399/screenshot/%7B4329461b-5e9c-4f8b-b589-ddc1af1d55a6%7D
Command duration or timeout: 16 milliseconds
Build info: version: '2.52.0', revision: '4c2593cfc3689a7fcd7be52549167e5ccc93ad28', time: '2016-02-11 11:22:43'
System info: host: 'mrunal-laptop', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=41.0.2, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: e796089b-1d64-4590-9157-a0716a57e399
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
    at org.openqa.selenium.remote.RemoteWebElement.getScreenshotAs(RemoteWebElement.java:447)
    at thirdsession.GetProperties.main(GetProperties.java:20)

Upvotes: 3

Views: 8280

Answers (4)

SivaPalepu
SivaPalepu

Reputation: 11

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;

Upvotes: 0

Andrew Regan
Andrew Regan

Reputation: 5113

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.

Upvotes: 5

callmepills
callmepills

Reputation: 722

The getScreenShotAs() method is not defined as part of the WebElement interface. Rather, it is included as part of the TakesScreenshot interface. If you want to take a screenshot and save it to a file, try the following:

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

Upvotes: -2

Jyothishwar Deo
Jyothishwar Deo

Reputation: 436

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

Upvotes: -2

Related Questions