2015-06-20 28 views
7

我用這個例子來創建一個phantomjs代碼來登錄到網站。PhantomJS打開一個接一個的頁面

var page = require('webpage').create(); 
page.open("http://www.facebook.com/login.php", function(status) { 

    if (status === "success") { 
    page.onConsoleMessage = function(msg, lineNum, sourceId) { 
     console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")'); 
    }; 
    page.evaluate(function() { 
     console.log('hello'); 
     document.getElementById("email").value = "email"; 
     document.getElementById("pass").value = "password"; 
     document.getElementById("u_0_1").click(); 
     // page is redirecting. 
    }); 
    setTimeout(function() { 
     page.evaluate(function() { 
     console.log('haha'); 
     }); 
     page.render("page.png"); 
     phantom.exit(); 
    }, 5000); 
    } 
}); 

來自此鏈接。 https://gist.github.com/ecin/2473860

但我想打開另一個按鈕的鏈接或直接在它上面。我該怎麼做?

這是一個更簡單的例子。不起作用...

var page = require('webpage').create(); 
var url = "www.example.com"; 

page.open(url, function (status) { 

    setTimeout(function() { 
     page.evaluate(function() { 
      console.log('haha'); 
     }); 
     page.render("example.png"); 
     phantom.exit(); 
    }, 5000); 

}); 



var url = "www.google.com"; 

page.open(url, function (status) { 

    setTimeout(function() { 
     page.evaluate(function() { 
      console.log('haha'); 
     }); 
     page.render("google.png"); 
     phantom.exit(); 
    }, 5000); 

}); 
+0

注意比PhantomJS有一個不同於節點的執行環境。節點和PhantomJS之間有橋樑,但是它們寫法稍有不同。 –

回答

7

非常接近,現在將你的兩個片段合併爲一個。 page.open()是異步這就是爲什麼你需要打開下一個頁面第一個完成後才:

var page = require('webpage').create(); 
var url = "http://www.example.com"; 

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

page.open(url, function (status) { 
    page.onConsoleMessage = function(msg, lineNum, sourceId) { 
     console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")'); 
    }; 
    page.evaluate(function() { 
     document.getElementById("email").value = "email"; 
     document.getElementById("pass").value = "password"; 
     document.getElementById("u_0_1").click(); 
     // page is redirecting. 
    }); 

    setTimeout(function() { 
     page.evaluate(function() { 
      console.log('haha'); 
     }); 
     page.render("example.png"); 


     var url = "http://www.google.com"; 

     page.open(url, function (status) { 
      setTimeout(function() { 
       page.evaluate(function() { 
        console.log('haha'); 
       }); 
       page.render("google.png"); 
       phantom.exit(); 
      }, 5000); 
     }); 
    }, 5000); 
}); 

要真正看到裏面console.log()page.evaluate()您需要註冊到page.onConsoleMessage事件。還有更多其他事件在調試時很有用。

不要忘記將協議(http://或file:///)添加到您要打開的URL中。 PhantomJS在這方面有點挑剔。

而不是等待一段靜態的時間(setTimeout()),直到您執行某個操作後加載下一頁。您應該使用page.onLoadFinished事件。這對於導航密集型腳本來說很麻煩。對於較長的腳本使用CasperJS

通常情況下,Element.click()不起作用。 This question針對這些情況提供了許多解決方案。

+1

好的。這有效,但如果我想在谷歌搜索框中輸入內容並按下按鈕進行搜索,該怎麼辦?然後我得到渲染搜索結果? – macroscripts

+1

@macroscripts Google是一個非常糟糕的網站,開始嘗試PhantomJS。但是,您可以使用'page.evaluate()'來更改輸入字段的值並單擊[click](http://stackoverflow.com/questions/15739263/phantomjs-click-an-element)搜索按鈕。您也可以使用'page.sendEvent()'填充該字段,並希望爲PhantomJS啓用即時結果。 –

+0

我以谷歌爲例。我正在網站上做這個代碼。它有不同的領域。我必須從sub1.domain.com登錄並訪問sub2.domain.com。我想從中獲取一些數據。 你給我的代碼有效但不完全。它到達sub1但不能正確登錄。它轉到sub2,但我沒有看到我的用戶名登錄。 我將粘貼稍後使用的示例。 – macroscripts

相關問題