先用node安装
下载selenium-standalone server并运行
1
2
|
npm install webdriverjs java -jar path/to/your/selenium-server-standalone-2.39.0.jar |
创建test_webdriver.js
1
2
3
4
5
6
7
8
9
10
|
var
webdriverjs = require( ‘webdriverjs‘ );
var
options = { desiredCapabilities: { browserName: ‘chrome‘
} };
webdriverjs .remote(options)
.init()
.title( function (err, res) {
console.log( ‘Title was: ‘
+ res.value);
})
.end();
|
用node执行
1
|
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文件中加入代码:
1
2
|
var
System = require( ‘system‘ );
System.setProperty( "webdriver.chrome.driver" , "C:\Program Files (x86)\Google\Chrome\Application" );
|
重新运行,报错: can‘t find module ‘system‘
修改webdriver server的运行命令:
1
|
java -jar selenium-server-standalone-2.35.0.jar -Dwebdriver.chrome.driver= "D:\dev\chromedriver.exe"
|
删掉js中加入的代码,运行成功