Search code examples
javascriptseleniumautomationselenium-chromedriverchrome-options

Configured ChromeDriver capabilities are lost after building the WebDriver in Node Selenium


I'm trying to add in the default download path with chrome capabilities using the code shown below:

const test = async () => {
  let builder = await new Builder().forBrowser("chrome");
  let chromeCapabilities = builder.getCapabilities();

  // chromeCapabilities.set("download.default_directory", downloadFolder);
  chromeCapabilities.merge({ "download.default_directory": downloadFolder });

  console.log(chromeCapabilities.get("download.default_directory"));

  // builder.setChromeOptions(chromeCapabilities);
  builder.withCapabilities(chromeCapabilities);
  // builder.setChromeOptions({ "download.default_directory": downloadFolder });
  // builder.withCapabilities({ "download.default_directory": downloadFolder });

  console.log(builder.getCapabilities().get("download.default_directory"));

  const driver = builder.build();
  await driver.get(tempoboxLoginUrl);

  const driverCapabilities = await (await driver).getCapabilities();
  console.log(await driverCapabilities.get("download.default_directory"));
};

test();

After merging the capabilities with the new capability I want to add, I can log the value of the capability and it shows as expected. However once the driver is built, logging the value of the capability returns undefined. The following is the output when running this code with Node:

> node Test.js

C:\Users\dummy\Desktop      << first log
C:\Users\dummy\Desktop      << second log

DevTools listening on ws://127.0.0.1:57980/devtools/browser/d53946e4-cedc-4809-a20c-b8b3416463cc

undefined                   << third log

Solution

  • So I eventually figured it out. The capabilities must be added in a very specific way. See example here:

        const builder = new Builder().withCapabilities({
          browserName: "chrome",
          "goog:chromeOptions": {
            args: ["--start-maximized"],
            prefs: { "download.default_directory": downloadFolder },
          },
        });
    
        const driver = await builder.build();