WebElement#getScreenShotAs(OutputType.File) 不起作用



我正在尝试使用Firefox浏览器上的Selenium webdriver 2.47.0版本中添加的WebElement#getScreenShotAs(OutputType.FILE(功能法典

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

..但我得到以下异常:

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)

错误的真正原因是许多/大多数WebDriver实现实际上并不支持基于元素的屏幕截图,尽管WebElement自2.47.0以来扩展了TakesScreenshot。也许有一天,这种情况会改变。

如果你想要截图,你需要在整个浏览器级别进行,在这种情况下 - 就像其他答案一样 - 你需要传递WebDriver实例。

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

严格来说,您可能应该执行以下操作,因为并非所有驱动程序都保证支持屏幕截图,例如 HtmlUnitDriver .

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

单元素屏幕截图有替代解决方案,但它们不可避免地涉及裁剪完整浏览器屏幕截图。例如,请参阅:https://stackoverflow.com/a/13834607/954442

更新:只是为了澄清这不是一个错误,而是尽管元素屏幕截图是 W3C WebDriver 规范的一部分,但不同的浏览器具有不同级别的合规性/覆盖范围,据我所知,此功能仅受 Microsoft Edge 支持。

不要从建议中导入这些库,

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

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

导入这些库。

import org.openqa.selenium.OutputType;

应该是这样的。

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

将上面的代码替换为

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

getScreenShotAs() 方法未定义为WebElement接口的一部分。相反,它作为TakesScreenshot接口的一部分包含在内。如果要截取屏幕截图并将其保存到文件中,请尝试以下操作:

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

最新更新