java - 使用 selenium 和 java 启动 Electron 应用程序

标签 java electron selenium-chromedriver

我试图在 Windows 环境中使用 selenium 和 java 启动我的 Electron 可执行文件,但出现超时错误。

我使用的代码是这样的:

System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe"); 

ChromeOptions opt = new ChromeOptions();
opt.setBinary("C:\\Users\\myUser\\MyApp\\MyApp.exe");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("chromeOptions", opt);

WebDriver driver = new ChromeDriver(capabilities);

执行前面的代码时,我的应用程序已启动,但我无法继续测试,因为我从 selenium 收到此错误:

org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally
  (Driver info: chromedriver=2.33.506120,platform=Windows NT 10.0 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 61.21 seconds
......
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:159)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:184)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:148)

我尝试过不同版本的 chromedriver 和 selenium,但它不起作用。有谁知道可能是什么问题?

注意:在 HttpCommandExecutor 类中执行此行时会引发错误:

ProtocolHandshake.Result result = handshake.createSession(client, command); 

最佳答案

您需要为您的应用程序路径定义 chromeDriver args。我正在与您分享我的代码。希望能有所帮助。

 ChromeOptions options = new ChromeOptions();
    options.setBinary(binaryPath);
    options.addArguments("--app=" + argPath);
    options.setCapability("chromeOptions", options);
    driver = new ChromeDriver(options);    

关于java - 使用 selenium 和 java 启动 Electron 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47270634/

相关文章:

python - 我想使用 selenium 和 python 打开网站中的每个项目进行抓取

python-3.x - 协作实验室 : chromedriver is not included in PATH

electron - 致命的 : Running as root without --no-sandbox is not supported using Electron 7. 1.3。在 Debian 8、9 上

javascript - 在 Electron 中运行 Jest 并使用 --inspect-brk

java - webElement.click() 不起作用,但 action.click(webElement).build().perform();作品

java - 当数组大小 > 索引时为 "java.lang.ArrayIndexOutOfBoundsException: 1"

deployment - 如何导出 Electron 应用程序以在浏览器中使用

java - Java 中的并行文件下载到 U 盘

java - 膨胀布局 NullPointerException

java - CMU Sphinx 5prealpha(语音识别系统)安装