3

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)
Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71

4 Answers4

5

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.

Community
  • 1
  • 1
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
  • Ok..I guess this needs fixing on webdriver Side then..i'll take a look then..I just wanted to confirm if anyone here as been successful to get this working on any browser..i'll wait for some more users to look at this..if they've been successful in getting this working..thnx – Mrunal Gosar Feb 16 '16 at 07:02
  • This isn't a bug to be fixed, it's missing functionality - with the possible exception of the Edge driver. So if you need element images in other browsers you have to try the workaround. – Andrew Regan Feb 16 '16 at 12:19
  • Yes, it seems Edge does support element screenshots: https://dev.windows.com/en-us/microsoft-edge/platform/status/webdriver/details/, https://w3c.github.io/webdriver/webdriver-spec.html#take-element-screenshot – Andrew Regan Feb 16 '16 at 13:16
  • Did this answer your question? – Andrew Regan Mar 29 '16 at 00:06
0

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;

SivaPalepu
  • 11
  • 1
  • 2
-2

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);
callmepills
  • 672
  • 9
  • 13
  • "The getScreenShotAs() method is not defined as part of the WebElement interface" <- That's misleading, as WebElement extends TakesScreenshot. – Andrew Regan Feb 15 '16 at 20:13
  • @AndrewRegan Doh! It looks like this change was introduced with the 2.47.0 version as the OP clearly stated. :S Thanks for pointing that out. – callmepills Feb 15 '16 at 21:18
  • No problem. I tried using the same feature myself earlier in the year, before realising it was pretty much impossible. – Andrew Regan Feb 15 '16 at 21:19
-2

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);
Jyothishwar Deo
  • 436
  • 3
  • 10