0

Hi my selenium chromedriver in node automatically downloads files. I want to disable automatically downloads,

therefor I found many threads for python with a solution to set download_restrictions to 3, but in JS is no experimental settings I guess, and userPreferences seems to be the wrong place.

Someone know how to disable automatically downloads in a headless chrome in node.

Src: Disable all downloads with ChromeDriver and Selenium

https://chromeenterprise.google/policies/?policy=DownloadRestrictions https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc?view=markup

my example code, download directory seems to be working

import { Builder, Capabilities } from 'selenium-webdriver';
import * as Chrome from 'selenium-webdriver/chrome.js'

export async function getDriver() {
  const service = new Chrome.ServiceBuilder("./selenium/chromedriver");
  const chromeOpts = await new Chrome.Options()
      .addArguments("--disable-infobars")
      .addArguments("--headless")
      .setUserPreferences({
        "download_restrictions" : 3
      })

  const driver = new Builder()
      .withCapabilities(Capabilities.chrome())
      .setChromeService(service)
      .setChromeOptions(chromeOpts)
      .build();

  return driver;
}

const driver = await getDriver()
driver.get("http://www.africau.edu/images/default/sample.pdf")
R J Funnel404
  • 93
  • 1
  • 8

2 Answers2

0

As per the selenium webdriver js's documentation found here.

Sets the user preferences for Chrome's user profile. See the "Preferences" file in Chrome's user data directory for examples.

If we have to block all downloads, then going by the python example shared above. You need to set the chrome options as below :

const chromeOpts = await new Chrome.Options()
      .addArguments("--disable-infobars")
      .setUserPreferences({
        "download_restrictions" : 3
      })

I tried this for my case and I found this configuration to be working. Below are the logs that I found during the test execution :

Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1615982301.751][INFO]: [0ecc2308b910c768a3c91d84cdacbdd8] COMMAND InitSession {
   "capabilities": {
      "alwaysMatch": {
         "browserName": "chrome",
         "goog:chromeOptions": {
            "args": [ "--disable-infobars" ],
            "prefs": {
               "download_restrictions": 3
            }
         }
      }
   },
   "desiredCapabilities": {
      "browserName": "chrome",
      "goog:chromeOptions": {
         "args": [ "--disable-infobars" ],
         "prefs": {
            "download_restrictions": 3
         }
      }
   }
}

And below is the snippet of the response where I can see the preference got reflected :

   "dns_prefetching": {
      "enabled": false
   },
   "download_restrictions": 3,

Techrookie89
  • 380
  • 3
  • 14
  • hi thanks for the help unfortunately it did not work, I have added above in the main post a full code to test. TestSystem is Ubuntu 20.04 , maybe it has a other behavior on other machines? headless in important. – R J Funnel404 Mar 18 '21 at 11:19
0

find my own dirty hack and not a good solution but it works

{ "download.default_directory": "/dev/null", "download_restrictions": 3 }
R J Funnel404
  • 93
  • 1
  • 8