toadead
toadead

Reputation: 1108

How to Set File Download Directory Using Node.js Selenium Chrome Driver

I'm trying to specify a file download directory for my Node.js Selenium Chrome driver. Here is my code for creating the web driver:

var downloadFolder = '/Users/andrew/Desktop';
var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .setChromeOptions(new chrome.Options()
        .setUserPreferences({'download.default_directory': downloadFolder}))
    .build();

My experiment shows my downloaded file still goes to the default /Users/andrew/Download folder. Am I doing something wrong here? Using selenium-webdriver as my module by the way.

Upvotes: 2

Views: 6425

Answers (4)

Raksha Thakre
Raksha Thakre

Reputation: 13

I tried with the below two codes, yet the file is not getting saved to my specified path:

var downloadFolder = 'E:/CannonGroup/DownloadedAPfile';
var driver = new Builder()
    .forBrowser('chrome')
    .setChromeOptions(new chrome.Options()
        .setUserPreferences({'download.default_directory': downloadFolder}))
    .build();
var chromeCapabilities = webdriver.Capabilities.chrome();
var chromeOptions = {
    'prefs': {"download.default_directory":"C:/Raksha"}
};
chromeCapabilities.set('chromeOptions', chromeOptions);
var driver = new webdriver.Builder()
      .withCapabilities(chromeCapabilities)
      .build();

Upvotes: 0

Akshansh Verma
Akshansh Verma

Reputation: 51

for me this work .. or else u can go in /node_modules/selenium-webdriver/chrome.js check the functions.

let chrome = require('selenium-webdriver/chrome');
let { Builder } = require('selenium-webdriver');
var driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(new chrome.Options().setUserPreferences(
    { "download.default_directory": task.download_dir }
))
.build();

Upvotes: 4

Mohamed Yaser
Mohamed Yaser

Reputation: 21

I also had the same issue with specifying the download directory for Chrome driver using Node.js. Did some research online and found that prefs can be used in Chrome options and that there is a specific way of formatting it. The code below worked for me. Try it out and let me know.

var chrome = require('chromedriver');
var chromeCapabilities = webdriver.Capabilities.chrome();
const chromeOption = require('selenium-webdriver/chrome');
//setting chrome options to start the browser fully maximized
var chromeOptions = {
    'args': ['--test-type', '--start-maximized'],
    'prefs': {"download.default_directory":"/home/(user)/Downloads/Chrome_test"}
};
chromeCapabilities.set('chromeOptions', chromeOptions);
var driver = new webdriver.Builder()
  .withCapabilities(chromeCapabilities)
  .build();

Upvotes: 2

Jeremie
Jeremie

Reputation: 96

I had a similar issue using the chromedriver 2.30, if this is the case for you, try to update it to the (currently) latest 2.33 version

Upvotes: 0

Related Questions