【问题标题】:Take a screenShot of the whole page [duplicate]截取整个页面[重复]
【发布时间】:2019-10-31 20:14:58
【问题描述】:

我需要自动化拍摄网站整个页面的屏幕截图。我尝试了一个解决方案,但只占用了页面的一部分。有人可以帮忙吗? 下面你会看到我尝试过的代码。

public void captureScreenshot(WebDriver driver, String screenshotName) {
    // Take the screenshot only is the feature is activated
    if (isActivate) {

        try {
            // before to take the screenshot
            utils.sleep(1);

            TakesScreenshot ts = (TakesScreenshot) driver; 
            File source = ts.getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(source, new File(dirPath + fileSep + screenshotName
                + "_" + strDateStamp + ".png"));
            String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
            System.setProperty(ESCAPE_PROPERTY, "false");
            URL path = new File(dirPath + fileSep + screenshotName + "_"
                + strDateStamp + ".png").toURI().toURL();
            String test = "<a href=" + path + "> click to open the screenshot "
                + screenshotName + "</a>";
            Reporter.log(screenshotName + test + "<br>");
        } catch (Exception e) {
            System.out.println("Exception while taking screenshot " + e.getMessage());
        }
    }
}

【问题讨论】:

  • IEDriver 会截取完整的屏幕截图。 (不仅仅是页面的可见部分。)但是有些网站会在滚动时加载更多内容。

标签: java selenium


【解决方案1】:

可以使用 Shutterbug 完成:

https://www.assertthat.com/posts/selenium_shutterbug_make_custom_screenshots_with_selenium_webdriver

制作整页截图:

Shutterbug.shootPage(driver, ScrollStrategy.WHOLE_PAGE).save();

github上的来源https://github.com/assertthat/selenium-shutterbug

提供在 Chrome 中制作整页截图的能力和一些其他扩展功能。

【讨论】:

    【解决方案2】:

    您好,您可能想尝试以下代码。它适用于我在 windows 中为 chrome 和 firefox 截取整个页面。

    public void getScreenshotOfWholePageWithScreenshotName(WebDriver driver, String path, String name) {
        try {
            Thread.sleep(2000);
            Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
            ImageIO.write(fpScreenshot.getImage(), "PNG", new File(path + "\\" + date() + " - " + name + ".png"));
            Thread.sleep(2000);
       } catch (Exception ex) {
            ex.printStackTrace();
       }
    }
    

    为了使用上述代码,您必须使用以下库:

    <dependency>
        <groupId>ru.yandex.qatools.ashot</groupId>
        <artifactId>ashot</artifactId>
        <version>1.5.2</version>
    </dependency>
    

    我正在使用 maven,因此我在 POM.xml 文件中添加了依赖项。

    干杯!

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 2012-02-21
      • 2018-01-19
      • 2013-07-27
      • 2016-08-31
      • 2019-01-11
      相关资源
      最近更新 更多