debuggingPhantomJS网页。开放失败
在PhantomJS中,webpage.open接受一个状态参数设置为“成功”或“失败”的callback。 根据文档,如果没有networking错误发生,将会“成功”,否则“失败”。 有没有办法看到导致失败的底层networking错误?
我试图加载的URL工作正常,当我把它放在我的浏览器中,当我得到'失败'的信息后,我看到了我的页面,我打电话之前,我打电话给webpage.open(所以我可以'只是忽略失败)。 我正在使用Phantom进行testing,所以理想情况下,我希望在webpage.open失败时(或者更好地让它永远不会失败!)轻松获得有用的错误信息。
find这篇文章,解释如何设置callback,以找出失败的根本原因: http : //newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a- phantomjs-页面加载失败/
基于该页面,您可以打印出如下错误:
page.onResourceError = function(resourceError) { console.error(resourceError.url + ': ' + resourceError.errorString); };
该页面将继续展示一个详细logging幻影的例子
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')); };