Search code examples
javaexceptionseleniumscreenshot

Exception when I try to save a screenshot in Java


I am working with selenium and I wrote a method that takes screenshot and saves it in specified folder, but it keeps throwing an exception. Why? Here is my code:

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

I am getting following exception:

Exception in thread "main" java.io.FileNotFoundException: C:\Screenshots\09-13-15_02:10:52.jpeg (The filename, directory name, or volume label syntax is incorrect)

Solution

  • You use the : to stop separate time, maybe that's an unacceptable character for a file name. Try with dots as the file system surely accepts well the dots