2017-03-17 1 views
0

私はwhileループでcasper.then()アクションをネストしようとしています。しかし、スクリプトはcasper.then()関数の中でコードを実行することはないようです。ループ内でのcasper.jsアクションのネスト

ここに私のコードは

casper.then(function() { 
     while (this.exists(x('//a[text()="Page suivante"]'))) { 

     this.then(function() { 
      this.click(x('//a[text()="Page suivante"]')) 
     }); 

     this.then(function() {   
      infos = this.evaluate(getInfos); 
     }); 

     this.then(function() { 
      infos.map(printFile); 
      fs.write(myfile, "\n", 'a'); 
     }); 
     } 
    }); 

私は何かが足りないのですか?

答えて

0

casper.thenはキュー内のステップをスケジューリングし、すぐには実行しません。前のステップが完了したときにのみ実行されます。親casper.thenには基本的にwhile(true)のコードが含まれているため、決して終了しません。

あなたはそれを再帰を使用してビットを変更する必要があります。

function schedule() { 
    if (this.exists(x('//a[text()="Page suivante"]'))) { 

    this.then(function() { 
     this.click(x('//a[text()="Page suivante"]')) 
    }); 

    this.then(function() {   
     infos = this.evaluate(getInfos); 
    }); 

    this.then(function() { 
     infos.map(printFile); 
     fs.write(myfile, "\n", 'a'); 
    }); 

    this.then(schedule); // recursive step 
    } 
} 

casper.start(url, schedule).run(); 
関連する問題