【发布时间】: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 会截取完整的屏幕截图。 (不仅仅是页面的可见部分。)但是有些网站会在滚动时加载更多内容。