WebElement #getScreenShotAs(OutputType.File)无法正常工作

我试图在Firefox浏览器代码中使用selenium webdriver 2.47.0版本中添加的WebElement#getScreenShotAs(OutputType.FILE)function

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

..但我得到以下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) 

错误的真正原因是许多/大多数WebDriver实现实际上并不支持基于元素的屏幕截图 ,尽管WebElementTakesScreenshot 开始扩展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规范的一部分,但不同的浏览器具有不同级别的合规性/覆盖率,据我所知,此function仅受Microsoft Edge支持 。

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

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

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

导入这些lib。

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