会不会是你的 Firefox 没有安装在默认位置?我的安装在 中C:\Users\(username)\AppData\Local\Mozilla Firefox\
,我收到与您相同的错误消息。
浏览代码(感谢@achie),我发现以下内容node_modules\selenium-webdriver\firefox\index.js
:
* On Windows and OSX, the FirefoxDriver will search for Firefox in its
* default installation location:
*
* * Windows: C:\Program Files and C:\Program Files (x86).
* * Mac OS X: /Applications/Firefox.app
*
* For Linux, Firefox will be located on the PATH: `$(where firefox)`.
换句话说,在 Windows 上将 Firefox 目录放在 PATH 变量中甚至没有任何用处。
但源代码继续:
* You can configure WebDriver to start use a custom Firefox installation with
* the {@link Binary} class:
*
* var firefox = require('selenium-webdriver/firefox');
* var binary = new firefox.Binary('/my/firefox/install/dir/firefox-bin');
* var options = new firefox.Options().setBinary(binary);
* var driver = new firefox.Driver(options);
就我而言,这变成:
var firefox = require('selenium-webdriver/firefox');
var binary = new firefox.Binary('C:\\Users\\(username)\\AppData\\Local\\Mozilla Firefox\\firefox.exe');
var options = new firefox.Options().setBinary(binary);
var driver = new firefox.Driver(options);
现在 Selenium 找到了我的 Firefox,但我收到了下一条错误消息:
Error: Timed out waiting for the WebDriver server at http://127.0.0.1:53785/hub
我也试过var driver = new webdriver.Builder().forBrowser('firefox').setFirefoxOptions(options).build();
了,但这没有任何区别。
希望这可以进一步帮助您!