我有一个Node脚本,我想使用child_process模块来获得一个运行在PhantomJS的GhostDriver中的Selenium服务器。
我需要模块:Child = require "child_process"
下面是我如何启动服务器并将GD附加到服务器上(在Coffeescript中):
@Selenium = new Child.exec "java -jar selenium/selenium-server-standalone-2.44.0.jar -role hub -port 4444", (error, stdout, stderr) =>
console.log stdout
console.log error if error
@PhantomJS = new Child.exec "phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:4444", (error, stdout, stderr) =>
console.log stdout
console.log error if errorstdout for @PhantomJS是这样的:
PhantomJS is launching GhostDriver...
[ERROR - 2014-12-10T18:51:27.587Z] GhostDriver - main.fail - {"message":"Could not start Ghost Driver","line":82,"sourceId":4469911104,"sourceURL":":/ghostdriver/main.js","stack":"Error: Could not start Ghost Driver\n at :/ghostdriver/main.js:82","stackArray":[{"sourceURL":":/ghostdriver/main.js","line":82}]}此外,我还从该命令获得了以下错误:{"killed": false, "code": 1, "signal": null}
一些注意事项:
npm update只是想看看这会不会有什么区别"PORT_NUMBER=4444 | lsof -i tcp:${PORT_NUMBER} | awk 'NR!=1 {print $2}' | xargs kill",但没有结果。发布于 2016-06-14 13:49:16
如果其他人有此问题,我们使用daemon在后台运行子进程,以便终端可以自由运行其他命令/脚本。
备注:您需要从NPM:
npm install daemon --save-dev安装daemon模块 (它具有测试+良好的使用统计数据,并能满足您的需要/期望)
创建一个名为selenium_child_process.js的文件并粘贴以下代码:
console.log('Starting Selenium ...');
require('daemon')(); // this will run everything after this line in a daemon:
const exec = require('child_process').exec;
// note: your path to the selenium.jar may be different!
exec('java -jar ./bin/selenium.jar', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
if (stdout) {
console.log(`> ${stdout}`);
}
if (stderr) {
console.log(`>> ${stderr}`); // handle errors in your preferred way.
}
});然后使用node selenium_child_process.js运行文件(在您的终端中)
现在,selenium作为子进程在TCP端口4444上运行。
如果希望关闭Selenium服务器,则需要对进程进行kill。我们使用以下命令:
lsof -n -iTCP:4444 -sTCP:LISTEN -n -l -P | grep 'LISTEN' | awk '{print $2}' | xargs kill -9
如果你被困住了,我们很乐意帮忙!
https://stackoverflow.com/questions/27408864
复制相似问题