我對Phantomjs相當陌生,並開始瞭解如何使用它。然而,所有的半高級教程並沒有涵蓋我想要使用Phantomjs的東西。Phantomjs檢查是否存在javascript並且正在工作
現在我的問題是如何檢查Javascript是否在網站上處於活動狀態,以及它是否正常工作(即不在控制檯中拋出錯誤)。
我希望有人能夠指引我在正確的方向或知道如何做到這一點。
我對Phantomjs相當陌生,並開始瞭解如何使用它。然而,所有的半高級教程並沒有涵蓋我想要使用Phantomjs的東西。Phantomjs檢查是否存在javascript並且正在工作
現在我的問題是如何檢查Javascript是否在網站上處於活動狀態,以及它是否正常工作(即不在控制檯中拋出錯誤)。
我希望有人能夠指引我在正確的方向或知道如何做到這一點。
可以使用webpage.evaluate方法與打開的頁面交互:
var page = require('webpage').create();
page.open('http://m.bing.com', function(status) {
var title = page.evaluate(function(s) {
//what you do here is done in the context of the page
//this console.log will appear in the virtual page console
console.log("test")
//here they are just returning the page title (tag passed as argument)
return document.querySelector(s).innerText;
//you are not required to return anything
}, 'title');
console.log(title);
phantom.exit(); //closes phantom, free memory!
});
才能看到虛擬頁控制檯,你必須添加一個onConsoleMessage callback:
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
編輯:默認情況下, javascript被執行。
而不是標題有沒有一種方法,我可以得到所有JavaScript被加載的列表? –
你的意思是'
我認爲你正在尋找這個https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript – ep0
@ ep0看起來不像我需要從php –
調用phantomjs你是什麼意思「檢查一個JavaScript是否有效」? – fusio