我正在使用简单的 NodeJS 脚本来测试 google.com。使用 phantomJS 浏览器。
var webdriver = require('selenium-webdriver');
var assert = require('assert');
var driver = new webdriver.Builder().
withCapabilities({
browserName : "phantomjs"
}).
usingServer('http://127.0.0.1:4444/wd/hub').build();
//driver.manage().timeouts().implicitlyWait(60000);
driver.get('http://www.google.com');
driver.findElement(webdriver.By.xpath('//*[@id="gbqf"]')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.getTitle().then(function(title) {
assert.equal("webdriver - Google Search", title);
});
driver.quit();
使用运行独立的 selenium 服务器
> java -jar selenium-server-standalone-2.35.0.jar
node
使用命令并行运行脚本。
我收到以下错误
> timers.js:103
> if (!process.listeners('uncaughtException').length) throw e;
> ^ UnknownError: Error Message => 'Unable to find element with xpath
> '//*[@id="gbqf"]'' caused by Request =>
> {"headers":{"Accept":"application/json,
> image/png","Connection":"Keep-Alive","Content-Length":"45","Content-Type":"application/json;
> charset=utf-8","Host":"localhost:17476"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"xpath\",\"value\":\"//*[@id=\\\"gbqf\\\"]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/440b8b90-0fc8-11e3-8024-5bdaea20ab82/element"}
> Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12
> 15:42:01' System info: os.name: 'Linux', os.arch: 'i386', os.version:
> '2.6.18-8.el5', java.version: '1.6.0_16' Driver info: driver.version: