无法启动Selenium + PhantomJS / GhostDriver作为subprocess

我有一个Node脚本,我想使用child_process模块来获取运行PhantomJS的GhostDriverSelenium服务器。

我需要模块: 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 error 

@PhantomJSstdout是这样的:

 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}

一些说明:

  • Selenium jar文件实际上位于selenium/selenium服务器独立版本2.44.0.jar
  • 我已经尝试npm update只是为了看看是否会有所作为
  • 我发现其他的东西可能在4444端口上运行,所以我继续运行"PORT_NUMBER=4444 | lsof -i tcp:${PORT_NUMBER} | awk 'NR!=1 {print $2}' | xargs kill"无济于事
  • 我已经尝试从源代码安装PhantomJS根据这个build议到相同的错误
  • 如果我在脚本之外单独运行这些命令,它一切正常

如果有其他有这个问题,我们使用daemon解决它在daemon运行subprocess,以便terminal可以自由运行其他命令/脚本。

注意 :您将需要从NPM npm install daemon --save-dev daemon模块: npm install daemon --save-dev
它有testing+体面的使用统计数据,并做你需要/期望

创build一个名为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运行该文件( 在您的terminal中

现在,您在TCP端口4444上将selenium作为subprocess( 后台 )进行运行。


如果要closures Selenium服务器,则需要终止该进程。 我们使用下面的命令:

lsof -n -iTCP:4444 -sTCP:LISTEN -n -l -P | grep 'LISTEN' | awk '{print $2}' | xargs kill -9

如果您遇到困难,我们很乐意帮助您!

Interesting Posts