Disable chrome download multiple files confirmation

后端 未结 9 1382
太阳男子
太阳男子 2020-12-02 01:57

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

相关标签:
9条回答
  • 2020-12-02 02:08

    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);
    
    0 讨论(0)
  • 2020-12-02 02:13

    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 讨论(0)
  • 2020-12-02 02:18

    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 );

    0 讨论(0)
  • 2020-12-02 02:18

    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.

    0 讨论(0)
  • 2020-12-02 02:19

    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

    0 讨论(0)
  • 2020-12-02 02:20

    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.

    0 讨论(0)
提交回复
热议问题