首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >获取整个页面的screenShot

获取整个页面的screenShot
EN

Stack Overflow用户
提问于 2019-06-18 16:27:06
回答 2查看 692关注 0票数 0

我需要自动化的射手,采取整个网站的屏幕截图。我尝试了一个解决方案,但只取走了页面的一部分。有人能帮上忙吗?下面你可以看到我尝试过的代码。

代码语言:javascript
复制
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());
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2019-06-18 17:08:10

嗨,你可能想试试下面的代码。它可以在chrome和firefox的窗口中截取整个页面的截图。

代码语言:javascript
复制
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();
   }
}

为了运行上面的代码,你必须使用下面的库:

代码语言:javascript
复制
<dependency>
    <groupId>ru.yandex.qatools.ashot</groupId>
    <artifactId>ashot</artifactId>
    <version>1.5.2</version>
</dependency>

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

干杯!

票数 0
EN

Stack Overflow用户

发布于 2019-06-18 17:29:50

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56644802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档