2016-10-26 7 views
0

私はphantomjs-nodeで 'waitfor'を実装していますが、実際に評価するべきときと比較して大きな遅れがあるようです。私は、コンテンツの価値とコンテンツのログを真であると評価するものをログに記録していることを以下に見てきますが、事実の後10秒ほど良いことはないようです。phantomjs-node page.evaulateがハングしているようです

この遅延を引き起こす原因は何か、またはより良い評価方法があれば、

let Promise = require('bluebird'); 
let phantom = require('phantom'); 
let sitepage; 
let phInstance; 

phantom.create() 
    .then(instance => { 
     phInstance = instance; 
     return instance.createPage(); 
    }) 
    .then(page => { 
     sitepage = page; 
     return page.open('https://thepiratebay.org/search/game/0/99/0'); 
    }) 
    .then(status => { 

     return waitUntil(function() { 

      //This returns the correct content after a short period, while the evaluate ends up taking maybe 10s longer, after this content should evaluate true. 
      sitepage.property('content').then(content => { 
      console.log(content); 
      }); 

      return sitepage.evaluate(function() { 
       return document.getElementById('searchResult'); 
      }); 

     }).then(function() { 
      return sitepage.property('content'); 
     }).catch(Promise.TimeoutError, function(e) { 
      sitepage.close(); 
      phInstance.exit(); 
     }); 

    }) 
    .then(content => { 
     console.log('content'); 
     console.log(content); 
     sitepage.close(); 
     phInstance.exit(); 
    }) 
    .catch(error => { 
     console.log(error); 
     phInstance.exit(); 
    }); 


var waitUntil = (asyncTest) => { 
    return new Promise(function(resolve, reject) { 
     function wait() { 
      console.log('--waiting--'); 
      asyncTest().then(function(value) { 
       if (value) { 
        console.log('resolve'); 
        resolve(); 
       } else { 
        setTimeout(wait, 500); 
       } 
      }).catch(function(e) { 
       console.log('Error found. Rejecting.', e); 
       reject(); 
      }); 
     } 
     wait(); 
    }); 
} 

答えて

0

私の知る限り、関数sitepage.evaluateはElementを返します。私の経験では、それは悪い考えです。あなたはsitepage.evaluateから短い文字列を返すことをお勧めします。例えば、return document.getElementById( 'searchResult')の代わりに;あなたは書くことができる:

return document.getElementById('searchResult') ? 'true' : ''; 
関連する問題