截图整个页面

时间:2019-06-18 08:27:06

标签: java selenium

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

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

2 个答案:

答案 0 :(得分:0)

嗨,您可能想尝试以下代码。它适用于我在chrome和Firefox中在Windows中获取整个页面的屏幕截图。

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文件中。

干杯!

答案 1 :(得分:0)

可以使用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和其他一些扩展功能中制作完整屏幕截图的功能。

相关问题