在 Node Selenium 中构建 WebDriver 后配置的 ChromeDriver 功能丢失

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

我正在尝试使用下面显示的代码添加具有 chrome 功能的默认下载路径:

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

将功能与我要添加的新功能合并后,我可以记录功能的值,它按预期显示。然而,一旦构建了驱动程序,记录功能 returns 未定义的值。以下是 运行 此代码与 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

所以我最终弄明白了。必须以非常具体的方式添加功能。请参阅此处的示例:

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

    const driver = await builder.build();