2016-03-20 14 views
2

このコードをノードサーバーで使用できるように変換するのは苦労します。したがって、このコードはPhantomJSプロセス(つまり$:phantomjs index.js)で実行するように記述されていますが、パッケージrequire( "phantom")を使用してノードサーバで実行します。私はこれらの2つのコールバックが動作するのに問題があります。PhantomJSのファントムノードパッケージに相当するコード

page.onLoadFinished = function(status){ 
    console.log("Load Finished"); 
}; 
page.onUrlChanged = function(){ 
    console.log("URL Changed"); 
}; 

ここでは、状況全体をnodefyしようとする私の哀れな試みです。

phantom.create(['--ignore-ssl-errors=yes','--load-images=no']).then(function(ph) { 
    console.log("here"); 
    ph.createPage().then(function(page) { 
     page.property('onResourceRequested', function(requestData, networkRequest) { 
     console.log(requestData.url); 
     }); 
     page.open('https://example.com/login').then(function(status) { 
     console.log(status); 
     if (status !== 'success') { console.log("failed connection")} else { 

      page.evaluate(function() { 
      document.getElementById('email').value = "stuff"; 
      document.getElementById('password').value = "things"; 
      setTimeout(document.getElementsByTagName('button')[0].click(),5000); 
      console.log("login attempt"); 
      setTimeout(document.URL, 2000); 
      }); 
      page.onLoadFinished = function(status){ 
      console.log("Load Finished"); 
      }; 
      page.onUrlChanged = function(){ 
      console.log("url changed"); 
      }; 

     } 
     }); 
    }); 
    }); 

はまた、コードは、ファントムがログインした後、私はonUrlChangedを使用しようとやってonLoadFinishedた、次のページからのデータを必要とするが、問題があり、動作し、ページを取得し、ボタンをクリックします。

+0

明確にするために、nodejsのphantom.createメソッドの.onLoadFinishedおよび.onUrlChangedに設定した関数は決して実行されません。 –

答えて

2

page.onLoadFinishedとpage.onUrlChangedは、ページを開いた後にを実行したコールバック関数です。したがって、URLを開く前にそれらを割り当てることは意味があります。

また、Webページからconsole.logとエラーメッセージを購読することは便利な習慣です。

var phantom = require('phantom'); 
    phantom.create(['--ignore-ssl-errors=yes','--load-images=no']).then(function(ph) { 
    console.log("here"); 
    ph.createPage().then(function(page) { 

     page.property('onError', function(msg, trace) { 
      var msgStack = ['ERROR: ' + msg]; 
      if (trace && trace.length) { 
      msgStack.push('TRACE:'); 
      trace.forEach(function(t) { 
       msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : '')); 
      }); 
      } 
      console.error(msgStack.join('\n')); 
     }); 

     page.property('onConsoleMessage', function(msg, lineNum, sourceId) { 
     console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")'); 
     }); 

     page.property('onResourceRequested', function(requestData, networkRequest) { 
     console.log(requestData.url); 
     }); 

     page.property('onLoadFinished', function(status) { 
     console.log("Load Finished with status " + status); 
     }); 

     page.property('onUrlChanged', function(targetUrl) { 
     console.log("URL changed to: " + targetUrl); 
     }); 

     page.open('https://example.com/login').then(function(status) { 

     if (status !== 'success') { console.log("failed connection")} else { 

      page.evaluate(function() { 
       document.getElementById('email').value = "email"; 
       document.getElementById('password').value = "password"; 

       setTimeout(function(){ 
       console.log("login attempt"); 
       document.getElementsByTagName('button')[0].click(); 
       }, 5000); 
      }); 

      }); 

     } 
     }); 
    }); 
    }); 
+0

あなたは素晴らしいですね。私が持っていた問題は、私がphantomjsバージョンを使用していたpage.property( '')を使用していなかったことでした。 –

+0

残念ながら私はあなたの答えをupvoteするために適切な評判が足りない。Sorry bro。 –

関連する問題