難道您的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)`.
換句話說,它甚至沒有任何用處把Firefox的目錄在Windows 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);
現在硒發現我的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();
,但是沒有什麼區別。
希望這可以幫助你更進一步!