2016-07-01 6 views
1

casperJS startメソッドでランダムページを開こうとしましたが、一部のページが正しく読み込まれていて、一部が読み込まれていないため、このシナリオではcasperjsから抜け出せません。 これはコンソールにスタックしていて、CTR + Cを使用してコンソールから手動で終了する必要があります。CasperJS:exit not working

casper.start("some url", function() { 

    if(this.status().currentHTTPStatus == 200) { 
     casper.echo("page is loading"); 
    } else { 
     casper.echo("page is in error "); 
     this.exit(); 
    } 

}); 
+1

はあなたのコードで 'casper.run()'を呼び出していますか? – Sayakiss

+0

あなたの回答をありがとう、私はthis.exitを以下のように使用しています。 casper.run(function(){this.exit();});いくつかのページが応答していない場合や読み込みに時間がかかる場合は、スタックされている、私はこれを強制的に終了したい。 – DJJ

+0

指定された時間枠内にページが読み込まれていない場合、タイムアウトを設定する方法はありますか? – DJJ

答えて

2

グローバルstepTimeoutオプションを使用してそれをラップします。

サンプルコード:

var casper = require('casper').create({ 
    stepTimeout: 10000 //10s 
}) 

casper.start() 

casper.then(funtion(){ 
    casper.open(url) 
}) 

casper.run() 
+0

ありがとう、このソリューションは私のために働いています:) – DJJ

+0

@DJJようこそ。それ以上の質問がない場合は、私の答えを受け入れるように設定することができます... – Sayakiss

+0

done !!!ありがとう。 – DJJ

0

その後、 Sを無視するbypass()を試してみてください。

casper.start("some url", function() { 
    if(this.status().currentHTTPStatus == 200) { 
     casper.echo("page is loading"); 
    } else { 
     casper.echo("page is in error "); 
     this.bypass(2); // Will not execute the then functions. 
    } 
}).then(function() { 
    // The 1st then function. 
}).then(function() { 
    // The 2nd then function. 
}) 

casper.run(function() { 
    this.echo('Something'); 
    this.exit(); // <--- Here. 
}); 
+0

あなたの応答をありがとうが、私は以下のようにthis.exitを使用しています。 casper.run(function(){ \t this.exit(); });一部のページが応答されない場合、強制的に終了しません。 – DJJ

+0

私は答えを編集し、まだ別の解決策が必要かどうか教えてください。 – Anson

+0

指定された期間内にページが読み込まれない場合、タイムアウトを設定する方法はありますか? – DJJ