6

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);
Sarath
  • 1,438
  • 4
  • 24
  • 40
  • do you mean a window pops up, like save as ? – dumbPotato21 May 23 '17 at 20:38
  • @ChandlerBing I do not want anything to be dowloaded. Just disable automatic downloading of file. On setting the pref 'download.prompt_for_download' to true, i see the file save dialog appearing. But what i want is, I do not want the file to be dowloaded at all. – Sarath May 23 '17 at 20:41
  • Can you get me an example of a site which downloads automatically? Thanks – undetected Selenium May 24 '17 at 03:52
  • @Dev its an internal site actually. Won't be able to share it. – Sarath May 24 '17 at 13:22
  • @Sarath So what are you expecting when you simply try to hit a download link like this (http://selenium-release.storage.googleapis.com/3.4/IEDriverServer_Win32_3.4.0.zip) ? Thanks – undetected Selenium May 24 '17 at 13:30
  • @Dev I am not clicking on any download link. The moment i land on the page (eg., www.google.com), a file is starting to download. I want to skip/block this automatic downloading of file. Is there any capability that i can set in Selenium to achieve this? – Sarath May 24 '17 at 13:48
  • @Sarath There are certain options available but you need to guide me with a sample URL which does that. Thanks – undetected Selenium May 24 '17 at 13:50
  • @Dev we have an internal app, www.stage.cr.com. The moment i open the page using ChromeDriver (manually also), an apk file will start to download automatically. How to avoid this from downloading at all. – Sarath May 24 '17 at 14:10
  • @Sarath Do you mean the google analytics sites? e.g. gstatic.com, google-apis.com ? – undetected Selenium May 24 '17 at 14:10
  • @Dev No. Its an internal site. – Sarath May 25 '17 at 17:43
  • @Sarath If you can point me to a demo website then I can provide you a solution. Thanks – undetected Selenium May 25 '17 at 18:55
  • @DebanjanB I know this is a bit old, but I am encountering the same issue. When visiting the link to the static document below, chromedriver will save the file to my download folder. Is there a way to avoid this default behavior ? https://www.hudoig.gov/sites/default/files/documents/2016-FW-1007.pdf – Leo Bouloc Dec 26 '19 at 16:53
  • @LeoBouloc Can you raise a new question as per your new requirement? Stackoverflow volunteers will be happy to help you out. – undetected Selenium Dec 26 '19 at 17:10
  • Thank you, I posted my question here: https://stackoverflow.com/questions/59491516/disable-all-downloads-with-chromedriver-and-selenium – Leo Bouloc Dec 26 '19 at 17:26
  • @Sarath did you managed to solve this? I literally tried every solution in the internet, none of them worked! – badger Oct 22 '22 at 07:58

2 Answers2

0

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);
YourHelper
  • 703
  • 7
  • 17
0

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

Abhishek Dhoundiyal
  • 1,359
  • 1
  • 12
  • 19