2
var page = require("webpage").create(),

     loginPage = "https://subscriber.hoovers.com/H/login/login.html";

page.onConsoleMessage = function (msg) {

     console.log(msg);
};

page.open(loginPage);

page.onLoadFinished = function (status) {

    var url = page.url;
    console.log("Status:  " + status);
    console.log("Loaded:  " + url);
    pageload(url);
    page.render('url.png');

}

function pageload(url) {

    if (url == loginPage) {

        console.log("From loginPage");

        page.evaluate(function () {

            console.log("From pageload");

        });
    }
}
4

3 回答 3

1

漏洞!

解决方法是分配:

window.console.log = function(msg) { alert(msg) }; 

在页面内。评估

然后使用:

page.onAlert = function(msg) {
  console.log(msg);
};

赶上警报

于 2013-07-19T04:36:39.890 回答
-1

确保您的 phantomjs 设置不会禁用执行 javascript

page.settings.javascriptEnabled = true;
于 2014-07-10T07:14:28.457 回答
-1

由于不明原因(WebKit 或 PhantomJs 中的错误),console.log 在 page.evaluate 中不起作用。

您应该更改代码以使用 console.info、console.warn 或 console.error。

var page = require("webpage").create();
var loginPage = "https://subscriber.hoovers.com/H/login/login.html";

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

page.open(loginPage);

page.onLoadFinished = function (status) {

    var url = page.url;
    console.log("Status:  " + status);
    console.log("Loaded:  " + url);
    pageload(url);
    page.render('url.png');
    phantom.exit(0);
}

function pageload(url) {

    if (url == loginPage) {
        console.log("From loginPage");
        page.evaluate(function () {
            console.info("From pageload");
        });
    }
}
于 2013-06-03T07:46:52.437 回答