2012-04-11 7 views
3

私はphantom.jsで新しく、Webページをナビゲートしてリンクをクリックしようとしています(AJAX関数を呼び出し、ドキュメントHTML)にphantom.jsを追加します。ここでphantom.jsのリンクをクリックしてドキュメントを取得します。

は私のコードです:

window.setTimeout(function(){ 
    phantom.exit(); 
}, 120000); 

var page = require('webpage').create(); 

page.open("http://example.com", function(status) { 
    if (status !== 'success') { 
     console.log('{"error":"Unable to load the address for page"}'); 
     phantom.exit(); 
    } 

    var action = page.evaluate(function() { 
     document.getElementById("anID").click(); 
     return "clicked"; 
    }); 

    var results = page.evaluate(function() { 
     return document.documentElement.innerHTML; 
    }); 

    console.log(action); 

    window.setInterval(function() { 
     console.log(results); 
     phantom.exit(); 
    }, 3000); 
}); 

私の "アクション" 機能では、クリック()の呼び出しは、そのエラーが3回繰り返さ上げているように私は非常に混乱だ:また

TypeError: 'undefined' is not a function
phantomjs://webpage.evaluate():3 phantomjs://webpage.evaluate():1
ph.js:121 null

TypeError: 'undefined' is not a function
phantomjs://webpage.evaluate():3 phantomjs://webpage.evaluate():1
ph.js:121 null

TypeError: 'undefined' is not a function
phantomjs://webpage.evaluate():3 phantomjs://webpage.evaluate():1
ph.js:121 null

をクリックを送信したときにその行にコメントを付けると、アクション関数はエラーを発生させず、「クリックされた」コンソールログを返します。しかし3回...

私は間違っていますか?

ありがとうございます。

+2

FYIあなたは、この行の後に2セミコロンを持っています。 'のdocument.getElementById(「ANID」)をクリックします();;'それはだ –

+0

はいおかげでここに投稿する際の間違い、それでも問題があります。 – ronnieonrails

答えて

4

私は最終的にこのコードで自分の仕事を達成することができました。私は、クリック操作をスキップし、AJAX呼び出しに直接行ってきました:

// phantomjs test.js 'http://www.example.com' 'anID' 

    var system = require('system'); 
    var page = require('webpage').create(); 
    var url = system.args[1]; 
    var target = system.args[2]; 

    page.onConsoleMessage = function (msg) { 
     console.log(msg); 
     phantom.exit(); 
    }; 

    page.open(url, function (status) { 
      function evaluate(page, func) { 
       var args = [].slice.call(arguments, 2); 
       var fn = "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify(args) + ");}"; 
       return page.evaluate(fn); 
      } 
      page.injectJs('jquery-1.7.2.min.js'); 
      if (status === 'success') { 
        evaluate(page, function(target) { 
         $.ajax({ 
          type: 'POST', 
          url: document.URL, 
          data: "__EVENTTARGET=" + target, 
          success: function(msg){ 
           console.log(msg); 
          } 
         }); 
      }, target); 
     } 
    }); 
関連する問題