Is it possible to disable file download in chrome using selenium

Viewed 2150

On visiting some pages using ChromeDriver and Selenium, downloading of a particular file is happening automatically. Because of the file download, the code is not proceeding further. Is it possible to disable downloading of file using any ChromeOptions or preferences that can be set while creating ChromeDriver.

Tried the following ChromeOptions, but none helped.

prefs.put("download.default_directory", "NUL");
prefs.put("download.prompt_for_download", false);
prefs.put("profile.default_content_setting_values.automatic_downloads", 0);
2 Answers

Not sure if it will work but you can try this:

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("download.prompt.for.download",false);
ChromeDriver driver = new ChromeDriver(options);

In chrome 'Allow Download Restrictions' has the following 4 options:

  • 0= No Special restrictions
  • 1= Block dangerous downloads
  • 2= Block potentially dangerous downloads
  • 3= Block all downloads
  • 4 = Block malicious downloads

Code:

 ChromeOptions options = new ChromeOptions();
 Map<String, Object> prefs = new HashMap<>();
 prefs.put("download_restrictions", 3);
 options.setExperimentalOption("prefs", prefs);

 WebDriver driver = new ChromeDriver(options);

#Ref: https://chromeenterprise.google/policies/#DownloadRestrictions

Related