java - WebElement#getScreenShotAs(OutputType.File)不工作

标签 java selenium selenium-webdriver

我正在尝试在 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 实现 do not actually support element-based screenshots ,尽管 WebElement 扩展了 TakesScreenshot since 2.47.0 .也许有一天这会改变。

如果你想要屏幕截图,你需要在整个浏览器级别进行,在这种情况下 - 正如其他答案一样 - 你需要传递 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

更新:只是为了澄清这不是错误,尽管元素屏幕截图是 part of the W3C WebDriver spec ,不同的浏览器具有不同级别的合规性/覆盖率,据我所知,此功能仅为 supported by Microsoft Edge .

关于java - WebElement#getScreenShotAs(OutputType.File)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35416220/

相关文章:

java - 即使使用 selenium 中的 javascript 方法也无法更改 header 名称

java - 如何使用 selenium webdriver 切换到重定向 url

java - 我们可以在Elasticsearch的独立节点上安装插件来进行测试吗?

java - 作为模式的用户或所有者运行 DBUnit 之间有区别吗

java - 确定表单上项目的索引 (J2ME)

java - BrowserMob 代理和 Selenium 的问题

python - 使用 chromedriver 和 Selenium 创建 Python 可执行文件

java - 接受字符串而不丢失换行符,直到按下 -1

python - lxc容器中的 Selenium ; WebDriver异常: Message: invalid argument: can't kill an exited process

php - 如何使用 PHP 和 Facebook WebDriver 上传文件?