gpt4 book ai didi

phantomjs - 调试 PhantomJS 网页打开失败

转载 作者:行者123 更新时间:2023-12-02 19:27:50 55 4
gpt4 key购买 nike

在 PhantomJS 中,webpage.open 会使用状态参数设置为“成功”或“失败”的回调。根据文档,如果没有发生网络错误,则“成功”,否则“失败”。有没有办法查看导致失败的底层网络错误?

当我将其放入浏览器中时,我尝试加载的网址工作正常,当我在收到“失败”消息后截取屏幕截图时,我会看到在调用webpage.open之前我所在的页面(所以我不能忽视失败)。我正在使用 Phantom 进行测试,因此理想情况下,我希望有一种强大的方法,可以在 webpage.open 失败时轻松获取有用的错误消息(或者更好的是让它永远不会失败!)

最佳答案

找到这篇文章,其中解释了如何设置回调以了解失败的根本原因:http://newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a-phantomjs-page-load-fails/

根据该页面,您可以打印出如下错误:

page.onResourceError = function(resourceError) {
console.error(resourceError.url + ': ' + resourceError.errorString);
};

该页面继续显示幻影详细日志记录的示例

var system = require('system');

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'));
};

关于phantomjs - 调试 PhantomJS 网页打开失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16497868/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com