1

I can not take the screenshot. I am trying to save it in project path with folder name Screenshot

I have tried to change the path but still get the same error

public void getScreenShot() throws Exception {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\abidk\\Desktop\\chromedriver.exe");    
        WebDriver driver= new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com/");
        // store the webelement
        WebElement element_node = driver.findElement(By.xpath("//img[@id='hplogo']"));
        // pass the stored webelement to javascript executor
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("arguments[0].style.border='2px solid red'", element_node);
        Thread.sleep(1000);
         SimpleDateFormat dateFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
            Date date = new Date();
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(scrFile, new File("./Screenshot/" + "Google" +  "-" +dateFormatter.format(date)+".png"));

    }

I want to save the file in project path and call it ScreenShot

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Johnny
  • 61
  • 5

1 Answers1

0

The Problem is with the colon ":"

Windows doesn't allow files to be saved with : in file name.

Use dateFormatter.format(date).toString().replace(":", "-")

Example:

TakesScreenshot screenShotObj = ((TakesScreenshot) driver);
File sourceFile = screenShotObj.getScreenshotAs(OutputType.FILE);
Instant instant = Instant.now();
String toSavein="D:\\Screenshots";
String fileName = "ScreenShot" + instant.toString().replace(":", "-") + ".png";
File destinationFile = new File(toSavein, fileName);
FileUtils.copyFile(sourceFile, destinationFile);

Result: ScreenShot2020-01-18T11-20-58.238372400Z.png

Note: Instant instant = Instant.now(); is allowed only above Java 8

Siva
  • 1