Phantomjs检查javascript是否存在并且正在工作

Phantomjs check if javascript exists and is working

本文关键字:工作 存在 检查 javascript 是否 Phantomjs      更新时间:2023-09-26

我对Phantomjs很陌生,并且开始了解如何使用它。然而,所有的半高级教程都没有涵盖我想要使用Phantomjs的目的。

现在我的问题是,我该如何检查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回调:

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

EDIT:默认情况下执行javascript。