19

I developed a crawler with ruby watir-webdriver that downloads some files from a page. My problem is that when I click to download the second file, Chrome opens a bar in the top asking for confirmation that I am downloading multiple files from this website.

Once this is used by webdriver, I cannot confirm the download. Is there anyway to avoid this confirmation? I am thinking if is there any configuration to avoid it or if is there an extension to do this or even if I can click on the confirmation with webdriver.

thanks

0

9 Answers 9

20

I'm using Chrome 49 and none of the other solutions worked for me. After some research I found a working solution:

ChromeDriver createChromeDriverWithDownloadFolder(String folder) {
    Map<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", folder);
    chromePrefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1 );
    chromePrefs.put("download.prompt_for_download", false);

    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", chromePrefs);
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    cap.setCapability(ChromeOptions.CAPABILITY, options);
    return new ChromeDriver(cap);
}

It seems as if these settings are constantly changing. Therefore, here's how I found the right solution for my setup:

  1. Open Chrome and go to chrome://version/ to find the path of your profile
  2. In Default/Preferences is a json file called Preferences. Open it and search for automatic_downloads. In my case the interesting part of the file looked like this:

    ..."profile": { "avatar_bubble_tutorial_shown": 1, "avatar_index": 0, "content_settings": { "clear_on_exit_migrated": true, "exceptions": { "app_banner": {}, "auto_select_certificate": {}, "automatic_downloads": { "[.]localhost:63342,": { "setting": 1 },...

  3. From that I could derive that the right setting would be chromePrefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1 );

3
  • Thanks. Given solution worked for me on chrome 51 and chrome driver 2.20. Jul 26, 2016 at 12:46
  • Thanks it worked for me on the latest chrome browser and chrome driver version Oct 13, 2017 at 19:40
  • it is throwing bad_variant_access error for me, I am using puppeteer to launch chromium & download something from website. Aug 1, 2022 at 4:36
12

As of Chrome 56.0.2924.87, February 17, 2017, the only preference you need to set (however you set them for your webdriver) is:

'profile.default_content_setting_values.automatic_downloads': 1

Giving an updated answer because most answers here use outdated preferences or show other preferences that are unnecessary.

3
  • 1
    FYI.. this setting/option is still working with the newest Chrome version i.e.Version 83.0.4103.116 (Official Build) (64-bit). Tried and tested. Jul 2, 2020 at 22:11
  • this one works for me Aug 1, 2022 at 4:35
  • And apparently it's stopped working :|
    – Big Ian
    Oct 28, 2022 at 11:31
7

for new chrome (version 46 or newer) this options was changed now your hash must looks like this:

prefs = {
    'profile' => {
        'default_content_settings' => {'multiple-automatic-downloads' => 1}, #for chrome version olde ~42
        'default_content_setting_values' => {'automatic_downloads' => 1}, #for chrome newer 46
    }
}

browser = Watir::Browser.new :chrome, options: {prefs: prefs, args: ['--test-type', '--disable-infobars'}
2
  • This solution worked for me, but broke how I was originally handling browser window sizing. Is there a site that lists ALL the changes one can make?
    – kmancusi
    Apr 4, 2017 at 13:29
  • 2
    This solution is working in version 66 using chromedriver in selenium with perl.
    – nck
    May 22, 2018 at 9:06
3

Here is the solution for Java - Selenium implementation

We faced hard time fixing this, as we wanted to add automation test for functionality which downloads set of PDFs on a single download link.

Map<String, Object> prefs = new HashMap<String, Object>();
//To Turns off multiple download warning
prefs.put("profile.default_content_settings.popups", 0);
prefs.put( "profile.content_settings.pattern_pairs.*.multiple-automatic-downloads", 1 );
//Turns off download prompt
prefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOptions("prefs", prefs);
driver =  new ChromeDriver(options);

Hope this help to someone.

2
  • Do you have a javascript version? Of can I even do that from client-side using Javascript? All I need is setting multiple-automatic-downloads" to 1 (true) !! Because I do not want to see the prompt asking it.
    – Jenna Leaf
    Jan 26, 2018 at 16:38
  • 1
    No I dont have javascript version. Not sure but it should be possible. Jan 28, 2018 at 18:35
2

It seems that the solution is different for older and newer chromedriver versions and that is adding to the confusion.

chromedriver

profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = download_directory

b = Watir::Browser.new :chrome, :profile => profile

chromedriver2

prefs = {
      'profile' => {
          'default_content_settings' => {'multiple-automatic-downloads' => 1},
      }
  }

b = Watir::Browser.new :chrome, :prefs => prefs

Today most people are probably using chromedriver2 version and that is a solution that should work fine. It worked ok in my watir scripts as I am not getting the message: "This site is attempting to download multiple files. Do you want to allow this?" anymore.

1

Java solution:

cap = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
Map<String, Object> content_setting = new HashMap <>();

content_setting.put("multiple-automatic-downloads",1);

prefs.put("download.prompt_for_download", "false");
prefs.put("profile.default_content_settings", content_setting);

options.setExperimentalOption("prefs", prefs);
cap.setCapability(ChromeOptions.CAPABILITY, options);
1

this is what worked for me:

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("profile.default_content_setting_values.automatic_downloads", 1);
chromePrefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
0

This bug/enhancement has been raised in the chromedriver page at the below URL: http://code.google.com/p/chromedriver/issues/detail?id=130

Bug/Enhancement Status: Yet to be resolved.

1
  • Did you get any solution of it? Aug 2, 2013 at 11:53
0

I have tried to do it on page load client-side using markups.

<META HTTP-EQUIV="Content-Disposition" CONTENT="inline" />

It seems to work (it is working at this moment, in overriding).

But time will tell (might not have effect on future CHROME's, you know what I mean).

There are a list of available header fields published on a couple of sites which I find extremely helpful. Hope it will help you, as well.

https://www.w3.org/Protocols/HTTP/Issues/content-disposition.txt https://www.iana.org/assignments/cont-disp/cont-disp.xhtml#cont-disp-2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.