The Dot(.) operator is not working while trying to locate a file

340 views Asked by At

While taking screenshot in selenium, if I use the dot(.) operator to mention the destination folder path instead of the full path, then the code is returning the error :

java.io.FileNotFoundException: .\Screenshot\shot1.jpeg (The system cannot find the path specified)

I am using the dot operator for the folder variable. As per my understanding the dot means, it represents the project folder. However, if I use the actual path "F:/SeleniumRevisit/Screenshot/shot1.jpeg", the code is working without any problem. My project folder is SeleniumRevisit which is present in F: drive. Any help would be appreciated.

Code :

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.io.FileHandler;

public class Screenshot_getScreenshotAs 
{
    public static void main(String[] args) throws InterruptedException, IOException 
    {
        String key="webdriver.chrome.driver";
        String value="./Drivers/chromedriver.exe";
        System.setProperty(key,value);
        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://demo.guru99.com/test/simple_context_menu.html");
        Thread.sleep(2000);
        TakesScreenshot ts=(TakesScreenshot) driver;
        File src=ts.getScreenshotAs(OutputType.FILE);
        File dst=new File("./Screenshot/shot1.jpeg");       
        FileHandler.copy(src,dst);
    }
}
2

There are 2 answers

0
YaDav MaNish On

You can try with user.dir give the folder and screenshot name

   public static void fullPageScreenShot() {
   String screens = System.getProperty("user.dir") + 
    "./Screenshot/shot1.jpeg";
    File screenshot = 
         ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
    try {
        FileHandler.copy(screenshot, new File(screens));
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }
    //closing the webdriver
    driver.close();
    }
0
Force Bolt On

//For capture screenshot and save the destination please use this code. File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

    //BY try catch block.
    try {
        FileUtils.copyFile(screenshot, new 
      File("C:\\projectScreenshots\\homePageScreenshot.png"));
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }