【问题标题】:Unable to take browser full page screenshot无法获取浏览器整页截图
【发布时间】:2018-01-05 15:51:38
【问题描述】:

这是我的代码 代码1:

scenario.embed(sharedDriver.getScreenshotAs(OutputType.BYTES), "image/png");

代码2:

 BufferedImage image = null;
        try {
            image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            File file = new File("image.png");
            ImageIO.write(image, "png", file);
            scenario.embed(FileUtils.readFileToByteArray(file), "image/png");
        } catch (Exception e) {
            e.printStackTrace();
        }

代码 3:

  try {

            Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(2000)).takeScreenshot(sharedDriver);
            File file = new File("screenshot.png");
            ImageIO.write(screenshot.getImage(), "png", file);
            scenario.embed(FileUtils.readFileToByteArray(file), "image/png");
        } catch (Exception e) {
            e.printStackTrace();
        }

但以上代码都没有截取整页截图。它只需要可见的浏览器屏幕。 我正在linux机器上运行测试它。我没有在windows机器上测试。

【问题讨论】:

    标签: java selenium selenium-webdriver screenshot


    【解决方案1】:

    您没有提到您使用的是什么浏览器,但屏幕捕获行为对于不同浏览器的工作方式不同。只有 Firefox 能够在没有像 AShot 这样的辅助工具的情况下截取完整的屏幕截图。因此,您的“code1”块可能仅适用于 Firefox。

    您的“code2”块将无法工作,因为 Java 机器人库仅适用于物理屏幕内容,因此如果屏幕上没有出现任何内容,它将不可见。

    但是,AShot 应该适用于任何浏览器(至少适用于 Chrome 和 Firefox)。我认为您的代码有问题。尝试像这样更改您的代码:

    Screenshot screenCapture = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
    

    【讨论】:

    • 谢谢,实际上代码运行良好,但不适用于我的应用程序。我不知道为什么。是否有任何关键可以识别问题?
    • 我发现我的应用程序有一些带有公司名称的页脚,这是滚动和捕获的问题吗?
    • @DINESHKUMAR 只要此页脚或视口中的任何内容,这不会导致任何问题。所以你的页面的其余部分被捕获,这是唯一被排除的区域?
    • 它是冻结页脚。它显示在普通视图屏幕中。如果我使用鼠标滚动,页脚不会移动,只有内容会移动。我执行它在其他 Web 应用程序中运行良好的代码。但它在我们的应用程序中不起作用。
    • 因此,使用页脚捕获的图像,页面的其余部分不会在 chrome 和 firefox 中捕获。
    【解决方案2】:

    你可以试试下面的代码。它至少对我有用。

    // Get the entire page screenshot
            File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            BufferedImage fullImg = ImageIO.read(screenshot);
    

    【讨论】:

    • 这可能只适用于 Firefox。并非所有浏览器都能够开箱即用地捕获整页屏幕截图。
    • 可能的。但我们至少可以尝试
    猜你喜欢
    • 2015-06-10
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多