先用node安装
下载selenium-standalone server并运行
npm install webdriverjs
java -jar path/to/your/selenium-server-standalone-2.39.0.jar
创建test_webdriver.js
var webdriverjs = require('webdriverjs');
var options = { desiredCapabilities: { browserName: 'chrome' } };
webdriverjs
.remote(options)
.init()
.url('http://www.google.com')
.title(function(err, res) {
console.log('Title was: ' + res.value);
})
.end();
用node执行
node test_webdriver.js
报错:the path to the driver executable must be set by the webdriver.chrome.driver
解决:下载chromedriver https://code.google.com/p/selenium/wiki/ChromeDriver
在js文件中加入代码:
var System = require('system');
System.setProperty("webdriver.chrome.driver", "C:\Program Files (x86)\Google\Chrome\Application");
重新运行,报错: can't find module 'system'
修改webdriver server的运行命令:
java -jar selenium-server-standalone-2.35.0.jar -Dwebdriver.chrome.driver="D:\dev\chromedriver.exe"
删掉js中加入的代码,运行成功