WebElement # getScreenShotAs (OutputType.File) not working

I am trying to use the WebElement # getScreenShotAs (OutputType.FILE) function added to the version of selenium webdriver 2.47.0 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 get below exceptions:

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)
+3
source share
4 answers

The real cause of the error is that many / most WebDriver implementations do not actually support element-based screenshots , although it has been WebElementexpanding TakesScreenshot since version 2.47.0 . Perhaps someday this will change.

, , , , WebDriver.

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

, , , , , . HtmlUnitDriver.

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

, . . : fooobar.com/questions/119460/...

:, , , W3C WebDriver spec, /, , - Microsoft Edge.

+4

lib ,

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

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

.

import org.openqa.selenium.OutputType;

0

getScreenShotAs() WebElement. , TakesScreenshot. , :

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

- .

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

Source: https://habr.com/ru/post/1672381/


All Articles