gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:56 29 4
gpt4 key购买 nike

我正在尝试在 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com