javascript - 尝试截屏时出现 WebDriverException

标签 javascript java selenium-webdriver selenium-firefoxdriver

所以我试图从 Firefox 浏览器中截取屏幕截图,但出现以下异常:

org.openqa.selenium.WebDriverException: [Exception... "Data conversion failed because significant data would be lost"  nsresult: "0x80460003 (NS_ERROR_LOSS_OF_SIGNIFICANT_DATA)"  location: "JS frame :: resource://gre/modules/AsyncShutdown.jsm :: observe :: line 551"  data: no]
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'fd9ab02a8254', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-862.el7.x86_64', java.version: '1.8.0_232'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 72.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:buildID: 20200107212822, moz:geckodriverVersion: 0.26.0, moz:headless: true, moz:processID: 577, moz:profile: /tmp/rust_mozprofile3kZelv, moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, platformVersion: 3.10.0-862.el7.x86_64, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 7e2460a8-267c-4e91-9774-ad3fb5e4e808
    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:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:160)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658)
    at org.openqa.selenium.remote.RemoteWebDriver.getScreenshotAs(RemoteWebDriver.java:343)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ....

抛出它的java代码行:

文件 srcFile = ((TakesScreenshot) 驱动程序).getScreenshotAs(OutputType.FILE);

该代码在多台机器上运行良好,但在尝试在新机器上安装后,出现此错误

有人知道是什么原因造成的吗?我从容器运行应用程序

最佳答案

经过多次尝试,有一些方法可以帮助解决该问题:

  • 检查您尝试截取屏幕截图的元素的可见性。我试图拍摄一张 table 的照片,但有一些看不见的元素,它破坏了程序

    if (element.isDisplayed())

  • 制动元素并创建一个谜题:逐行取出并使用
  • 将所有元素组合在一起

BufferedImage bi = new BufferedImage(element.getSize().width, element.getSize().height, BufferedImage.TYPE_INT_RGB);

  • 等待元素加载:

    WebDriverWait 等待 = new WebDriverWait(driver, 300); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("report_table"))); wait.until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("返回 document.readyState").equals("complete"));

关于javascript - 尝试截屏时出现 WebDriverException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59918416/

相关文章:

javascript - 在 Javascript 中运行一个函数,但最后总是返回 'undefined'

JavaScript 内部 HTML

java - 在STS上运行Spring项目出现ServletException错误

javascript - 如何使用 Selenium 根据亚马逊上自动建议的某些建议将 "click"化?

java - 哪个 Selenium 版本支持 OperaDriver 版本?

php - 如何使用 uploadify 传递表单值

javascript - 在 Object.defineProperty 中调用父级实现

Java FX 场景生成器无法识别 css 自定义颜色常量(或变量)

java - 如何使用 doGet 方法捕获从 android 应用程序发送到 servlet 的 JSON 对象?

java - 面临使用 selenium webdriver 处理两个以上窗口的问题