使用命令行参数启动带有 Selenium WebDriver 的 Electron 应用程序

Start Electron application with Selenium WebDriver using command line arguments

我有一个 Electron 应用程序 (chat.exe),使用以下代码片段我可以使用 Chromedriver:

启动它
ChromeOptions options = new ChromeOptions();
options.setBinary(System.getenv("CHAT")); // CHAT = path to Chat.exe
driver = new ChromeDriver(options);

我的问题是:如何使用命令行参数启动它?

例如chat.exe -- --electronPort 5000 --webpackPort 3000 --accessToken 123456789

我尝试了以下方法但没有成功:

ChromeOptions options = new ChromeOptions();
options.setBinary(System.getenv("CHAT")); // CHAT = path to Chat.exe
options.addArguments("-- --electronPort 5000 --webpackPort 3000 --accessToken 123456789");
driver = new ChromeDriver(options);

显示以下错误:

org.openqa.selenium.WebDriverException: chrome not reachable

ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");

在非标准位置设置Chrome可执行文件,而不是设置chrome以外的任何其他可执行文件。因为您提供的不是 chrome 二进制文件(在您的情况下是 CHAT.exe)驱动程序打开 CHAT 而不是 chrome 浏览器并抱怨 chrome 无法访问。

Webdriver 仅用于自动化 Web 应用程序,不适用于独立应用程序自动化。为此,您可以使用其他工具,例如 "AutoIt".

以下格式有效(如@user861594 指定):

options.addArguments("<Key>=<value>");

但是,问题是 chromedriver 不能接受驼峰格式的参数。开发人员将 cli args 的语法从 camelCase 修改为连字符后,上述解决方案有效。

例如

options.addArguments("electron-port=5000");
options.addArguments("webpack-port=3000");
options.addArguments("access-token=12345‌​6789");