当我尝试在 Java 中保存屏幕截图时出现异常
Exception when I try to save a screenshot in Java
我正在使用 selenium,我编写了一个截取屏幕截图并将其保存在指定文件夹中的方法,但它一直抛出异常。为什么?
这是我的代码:
public void takeScreenshot(WebDriver driver) throws IOException{
Date date = new Date();
String fileName = new SimpleDateFormat("MM-dd-yy_HH:mm:ss").format(date).concat(".jpeg");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File destFile = new File(SCREENSHOTS.toString().concat(fileName));
FileUtils.copyFile(scrFile, destFile);
}
我遇到以下异常:
Exception in thread "main" java.io.FileNotFoundException: C:\Screenshots-13-15_02:10:52.jpeg (The filename, directory name, or volume label syntax is incorrect)
显然你是 运行 windows,所以:
您使用 : 来停止单独的时间,这可能是不可接受的文件名字符。尝试使用点,因为文件系统肯定能很好地接受点
我没有看到您在发布的代码中创建了目标文件,下面的代码应该可以做到。
File destFile = new File(SCREENSHOTS.toString().concat(fileName));
if (!destFile.exists()) {
destFile.mkdir();
}
您可以查看 this post 了解更多信息。
我正在使用 selenium,我编写了一个截取屏幕截图并将其保存在指定文件夹中的方法,但它一直抛出异常。为什么? 这是我的代码:
public void takeScreenshot(WebDriver driver) throws IOException{
Date date = new Date();
String fileName = new SimpleDateFormat("MM-dd-yy_HH:mm:ss").format(date).concat(".jpeg");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File destFile = new File(SCREENSHOTS.toString().concat(fileName));
FileUtils.copyFile(scrFile, destFile);
}
我遇到以下异常:
Exception in thread "main" java.io.FileNotFoundException: C:\Screenshots-13-15_02:10:52.jpeg (The filename, directory name, or volume label syntax is incorrect)
显然你是 运行 windows,所以:
您使用 : 来停止单独的时间,这可能是不可接受的文件名字符。尝试使用点,因为文件系统肯定能很好地接受点
我没有看到您在发布的代码中创建了目标文件,下面的代码应该可以做到。
File destFile = new File(SCREENSHOTS.toString().concat(fileName));
if (!destFile.exists()) {
destFile.mkdir();
}
您可以查看 this post 了解更多信息。