Java/Selenium - The filename, directory name, or volume label syntax is incorrect (java.io.IOException)

Viewed 1169

I have written a simple selenium script to capture screenshot and for some reason, this script is throwing the exception:

java.io.IOException: The filename, directory name, or volume label syntax is incorrect

public class ScreenshotUtil {

    WebDriver driver;

    public ScreenshotUtil(WebDriver driver) {
        this.driver = driver;
    }

    public String getScreenShot(String fileName) {
        SimpleDateFormat dFormat = new SimpleDateFormat("dd-MM-yyy HH-mm-ss");
        Date date = new Date();
        dFormat.format(date);
        TakesScreenshot ts = (TakesScreenshot) driver;
        File source = ts.getScreenshotAs(OutputType.FILE);
        String path = "./Screenshots/" + fileName + "_" + date + ".png";
        File destination = new File(path);
        try {
            FileUtils.copyFile(source, destination);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }
}
1 Answers

You use Unix-style paths on Windows.

Change your current path separator / to File.separator to achieve cross-platform execution for your code.

A quick fix for your code:

String path = "." + File.separator + "Screenshots" + File.separator + fileName + "_" + date + ".png";
Related