7

有没有办法调试 phantomjs 的 page.open 方法?我的应用程序加载了一些本地保存的文件,但不幸的是,打开页面时唯一可以获得的信息是它是否加载成功。更有趣的是,在浏览器中打开同一页面时会正确加载。

这是我的代码:

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

var openPage = function () {

    var url = 'http:\\localhost:53794/file.html';

    page.open(url, function (status) {
        if (status !== 'success') {
            console.log("FAIL:" + url);
            phantom.exit(2);
        }

        var date            = new Date().getTime();
        var outputFilename  = outputPath + 'print-' + date + '.png';

        setTimeout(function () {
            page.render(outputFilename);
            outputArray.push(outputFilename);

            setTimeout(function () {
                phantom.exit(1);
            }, 1);
        }, 1);        
    });
}

openPage();
4

2 回答 2

8

通过: http: //newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a-phantomjs-page-load-fails/

创建page变量后,但在调用之前page.open()添加以下代码:

page.onResourceError = function(resourceError) {
    page.reason = resourceError.errorString;
    page.reason_url = resourceError.url;
};

page.open()现在您可以在回调中打印出问题的原因,例如:

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

page.onResourceError = function(resourceError) {
    page.reason = resourceError.errorString;
    page.reason_url = resourceError.url;
};

page.open(
    "http://www.nosuchdomain/",
    function (status) {
        if ( status !== 'success' ) {
            console.log(
                "Error opening url \"" + page.reason_url
                + "\": " + page.reason
            );
            phantom.exit( 1 );
        } else {
            console.log( "Successful page open!" );
            phantom.exit( 0 );
        }
    }
);

调试功能

如果您进一步阅读博客,他会添加更多建议的事件处理程序。我将它们改编成一个函数,您可以使用该函数将事件处理程序注入您的页面对象(而不是在您的主代码中定义它们)

// this method injects some debugging event handlers 
// into a PhantomJS page object.
// usage:
//   var page = require('webpage').create();
//   var system = require('system');
//   addDebugEvents(page,system);
function addDebugEvents(page, system) {

    page.onResourceError = function (resourceError) {
        page.reason = resourceError.errorString;
        page.reason_url = resourceError.url;
    };

    page.onResourceRequested = function (request) {
        system.stderr.writeLine('= onResourceRequested()');
        system.stderr.writeLine('  request: ' + JSON.stringify(request, undefined, 4));
    };

    page.onResourceReceived = function (response) {
        system.stderr.writeLine('= onResourceReceived()');
        system.stderr.writeLine('  id: ' + response.id + ', stage: "' + response.stage + '", response: ' + JSON.stringify(response));
    };

    page.onLoadStarted = function () {
        system.stderr.writeLine('= onLoadStarted()');
        var currentUrl = page.evaluate(function () {
            return window.location.href;
        });
        system.stderr.writeLine('  leaving url: ' + currentUrl);
    };

    page.onLoadFinished = function (status) {
        system.stderr.writeLine('= onLoadFinished()');
        system.stderr.writeLine('  status: ' + status);
    };

    page.onNavigationRequested = function (url, type, willNavigate, main) {
        system.stderr.writeLine('= onNavigationRequested');
        system.stderr.writeLine('  destination_url: ' + url);
        system.stderr.writeLine('  type (cause): ' + type);
        system.stderr.writeLine('  will navigate: ' + willNavigate);
        system.stderr.writeLine('  from page\'s main frame: ' + main);
    };

    page.onResourceError = function (resourceError) {
        system.stderr.writeLine('= onResourceError()');
        system.stderr.writeLine('  - unable to load url: "' + resourceError.url + '"');
        system.stderr.writeLine('  - error code: ' + resourceError.errorCode + ', description: ' + resourceError.errorString);
    };

    page.onError = function (msg, trace) {
        system.stderr.writeLine('= onError()');
        var msgStack = ['  ERROR: ' + msg];
        if (trace) {
            msgStack.push('  TRACE:');
            trace.forEach(function (t) {
                msgStack.push('    -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
            });
        }
        system.stderr.writeLine(msgStack.join('\n'));
    };

}
于 2014-07-22T14:32:07.840 回答
2

您应该更改网址

http:\\localhost:53794/file.html

http://localhost:53794/file.html
于 2012-12-31T12:32:12.443 回答