您可能需要使用RemoteWebDriver接口。你使用Firefox還是IE?見下面
File screenshotFile = null;
try {
// This checks to make sure we're not running an
// incompatible driver to take a screenshot (e.g.
// screenshots are not supported by the HtmlUnitDriver)
//the following line will throw a ClassCastException if the current driver is not a browser typed driver
RemoteWebDriver compatibleDriver = (RemoteWebDriver) driver;
screenshotFile = ((TakesScreenshot) compatibleDriver).getScreenshotAs(OutputType.FILE);
} catch (ClassCastException e) {
//this driver does not support screenshots.
// this should get logged as a warning -- this only means the driver cannot be of type RemoteWebDriver
} finally {
if (screenshotFile == null) {
System.out.println("This WebDriver does not support screenshots");
return; //get us outa here
}
}
try {
String pathString = "some path of your choice"
File path = new File(pathString);
if (!path.exists())
path.mkdirs();
stringPath.concat("/someFileName.png");
File newFileLocation = new File(pathString);
System.out.println("Full path to screenshot: " + newFileLocation.getAbsolutePath());
FileUtils.copyFile(screenshotFile, newFileLocation);
} catch (IOException e) {
e.printStackTrace();
}
覺得我們只能採取截圖的網頁的可見部分,但我們沒有任何選擇採取截屏整網頁通過向下/向上滾動,甚至無法通過PrintScreen/SysRq –
感謝您的回覆,但爲了防火狐狸硒硒,我設法採取整個內容的屏幕截圖。但不能通過Java代碼 –