0

I want to download a file in chrome by "Save Link as" and change the filename and location of that file. Below is the code I have written so far to handle the windows pop-up. But in the directory, I am getting a temp file instead Any help would be appreciated.

    System.setProperty("webdriver.chrome.driver","C://Users//mansi.shrimali//workspace//Redmine_01//chromedriver01.exe");
    Properties pro= new Properties();
    FileInputStream fip =new FileInputStream(System.getProperty("user.dir") +"/src/Config.properties");
    pro.load(fip);

    String URL=pro.getProperty("BrowserURL");

    String downloadFilepath = "D:/Download";
    ChromeOptions options = new ChromeOptions();
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", downloadFilepath);
    options.setExperimentalOption("prefs", chromePrefs);
    //options.addArguments("--disable-extensions");

    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    cap.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(options);
Mansi
  • 63
  • 3
  • 11

1 Answers1

0

You can use java 8 Files class to move and rename a file after it is downloaded.

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

Path source = Paths.get("D:/Downloads/downloadedFileName.pdf");
Path newdir = Paths.get("C:/NewDir");
Files.move(source, newdir.resolveSibling("downloadedFileNewName.pdf"));

Below is the reference of the library https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html

Amit Jain
  • 4,389
  • 2
  • 18
  • 21