0

I need to download XLSX files using Java + Selenium in chrome, but I'm not able to choose the folder to download the files. I've tried all the other alternatives I found here, but from what I've noticed, all the others are already obsolete. Here is my code:

        String downloadFilepath = "D:\\down\\";

        HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        chromePrefs.put("prompt_for_download", false);
        chromePrefs.put("directory_upgrade", true);
        chromePrefs.put("download.default_directory", downloadFilepath);
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", chromePrefs);
        options.addArguments("--disable-notifications");
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        cap.setCapability(ChromeOptions.CAPABILITY, options);
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        driver = new ChromeDriver(options);
  • I use: prefs.put("download.default_directory", downloadDir); Other options are not necessary. (Make sure the folder exists and that you have proper permissions... I recommend a folder in the current user's user folder.) – pcalkins Aug 16 '21 at 20:55
  • Unfortunately for me it didn't work. Even when I indicate the project folder or even a folder inside the standard windows folder (downloads). I took out all the other options and left only the one you mentioned, but it doesn't work for me. – Camila Piva Aug 16 '21 at 22:51

1 Answers1

2

I managed to resolve it. I downloaded the last stable version that came out in July (mine was from June) of chromedriver and put the code below.

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(options);