3

只想从自己的样本开始学习 Selenium 测试。但是,在一切都设置好之后(Node.js,Selenium),当我想使用 selenium-webdriver 自己的示例时,它不起作用:

这是 selenium 提供的官方代码(名称为 google_search.js):

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .build();

driver.get('http://www.google.com/ncr');
driver.findElement(By.name('q')).sendKeys('webdriver');
driver.findElement(By.name('btnG')).click();
driver.wait(until.titleIs('webdriver - Google Search'), 1000);
driver.quit();

此代码在example文件夹中,在 Node.js cmd 中,我 cd 到 node_modules\selenium-webdriver\example,并将 geckodriver 放在此文件夹中,然后运行node google_search.js 结果如下:

C:\Users\username\node_modules\selenium-webdriver\lib\promise.js:2291
        throw error;
        ^

WebDriverError: Unable to parse new session response: {"error":"unknown error","
message":"Expected browser binary location, but unable to find binary in default
 location, no 'firefox_binary' capability provided, and no binary flag set on th
e command line"}
    at WebDriverError (C:\Users\username\node_modules\selenium-webdriver\lib\err
or.js:27:10)
    at C:\Users\username\node_modules\selenium-webdriver\lib\http.js:335:17
    at process._tickCallback (node.js:369:9)
From: Task: WebDriver.createSession()
    at Function.createSession (C:\Users\username\node_modules\selenium-webdriver
\lib\webdriver.js:366:24)
    at createGeckoDriver (C:\Users\username\node_modules\selenium-webdriver\fire
fox\index.js:497:27)
    at Driver (C:\Users\username\node_modules\selenium-webdriver\firefox\index.j
s:605:14)
    at Builder.build (C:\Users\username\node_modules\selenium-webdriver\index.js
:557:16)
    at Object.<anonymous> (C:\Users\username\node_modules\selenium-webdriver\exa
mple\google_search.js:44:6)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
From: Task: WebDriver.navigate().to(http://www.google.com/ncr)
    at Driver.schedule (C:\Users\username\node_modules\selenium-webdriver\lib\we
bdriver.js:414:17)
    at Navigation.to (C:\Users\username\node_modules\selenium-webdriver\lib\webd
river.js:1042:25)
    at Driver.get (C:\Users\username\node_modules\selenium-webdriver\lib\webdriv
er.js:832:28)
    at Object.<anonymous> (C:\Users\username\node_modules\selenium-webdriver\exa
mple\google_search.js:46:8)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)

我做错什么了吗?谁能告诉我为什么官方样本没有启动 Firefox?谢谢!

4

1 回答 1

3

尝试使用 Selenium 3.0.0 运行 C# 应用程序时出现相同的错误消息。

问题是我使用的是 x64 Geckodriver.exe。下载 32 位版本并将其复制到我的 bin 文件夹后,一切正常。

-罗伯特

于 2016-10-17T13:39:56.613 回答