2

I'm trying to download a file with Selenium Chromedriver through Electron. As we could not handle the popup window with selecting folder to download in, I tried to avoid this popup in this way:

prefs.put("download.prompt_for_download", false);

But it doesn't work. The full code is:

ChromeOptions options = new ChromeOptions();
HashMap<String, Object> prefs = new HashMap<>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", LocationUtil.getDownloadFolderPath());
prefs.put("download.prompt_for_download", false);
prefs.put("safebrowsing.enabled", false); // to disable security check eg. Keep or cancel button
options.setExperimentalOption("prefs", prefs);
ChromeDriver chromeDriver= new ChromeDriver(options);

Also tried to put these prefs through Capabilities but with no success.

((MutableCapabilities) chromeDriver.getCapabilities()).setCapability(ChromeOptions.CAPABILITY, options); Versions are:

  • ChromeDriver 80.0.3987.16
  • Selenium Java 3.141.59

How could I download the file in a specific directory without popup window in an Electron app? UPD: Tested with browser Chrome - all things are fine.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Filosssof
  • 1,288
  • 3
  • 17
  • 37

1 Answers1

3

Seems you were pretty close enough.

Apart from adding the preference of download.prompt_for_download to false you need to set a couple of more preferences as follows:

  • You need to add the type of file you wish you auto download. As an example to automatically download xml files you need to add:

    prefs.put("download.extensions_to_open", "application/xml");
    
  • You need to enable/disable safebrowsing.enabled as follows:

    prefs.put("safebrowsing.enabled", true);
    
  • Now you need to add the argument to safebrowsing-disable-download-protection

    options.addArguments("--safebrowsing-disable-download-protection");
    
  • Next, you need to add the argument to safebrowsing-disable-extension-blacklist

    options.addArguments("safebrowsing-disable-extension-blacklist");
    
  • Finally, to click on the element to initiate the download, you need to induce WebDriverWait for the elementToBeClickable().

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();
    

Clubbing it up altogether, as a demonstration, to download a xml file from the website your effective code block will be:

ChromeOptions options = new ChromeOptions();
HashMap<String, Object> prefs = new HashMap<>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", LocationUtil.getDownloadFolderPath());
prefs.put("download.prompt_for_download", false);
prefs.put("safebrowsing.enabled", true);
options.setExperimentalOption("prefs", prefs);
ChromeDriver chromeDriver= new ChromeDriver(options);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("--safebrowsing-disable-download-protection");
options.addArguments("safebrowsing-disable-extension-blacklist");
WebDriver driver =  new ChromeDriver(options); 
driver.get("http://www.landxmlproject.org/file-cabinet");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='MntnRoad.xml']//following::span[1]//a[text()='Download']"))).click();

Browser Snapshot:

ChromeDownload


References

You can find a couple of relevant reference discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352