1

PhantomJS のGhostDriverでSeleniumサーバーを実行child_processするためにモジュールを使用したい Node スクリプトがあります。

モジュールが必要です: 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

これは次のとおりですstdout@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}

いくつかのメモ:

  • 実際、Selenium jar ファイルは selenium/selenium-server-standalone-2.44.0.jar にあります。
  • npm updateそれが違いを生むかどうかを確認するためだけに試してみました
  • ポート 4444 で別の何かが実行されているのではないかと思いまし"PORT_NUMBER=4444 | lsof -i tcp:${PORT_NUMBER} | awk 'NR!=1 {print $2}' | xargs kill"た。
  • この提案に従ってソースからPhantomJSを同じエラーにインストールしようとしました
  • これらのコマンドをスクリプトの外で個別に実行すると、すべて正常に動作します
4

1 に答える 1

2

他の誰かがこの問題を抱えている場合に備えて、端末が他のコマンド/スクリプトを自由に実行できるようdaemonに、子プロセスをバックグラウンドで実行することで解決しました。

daemon: NPMからモジュールをインストールする必要がありますnpm install daemon --save-dev
:(テストと適切な使用統計があり、必要/期待することを行います

というファイルを作成し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ターミナルで)ファイルを実行します

これで、TCP ポート 4444 でselenium が子 (バックグラウンド) プロセスとして実行されます。


Selenium サーバーをシャットダウンする場合は、このプロセスが必要にkillなります。次のコマンドを使用します。

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

行き詰まった場合は、喜んでお手伝いします。

于 2016-06-14T13:49:16.103 に答える