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

URLを変更する必要があります

から

http:\\localhost:53794/file.html

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