javascript 如何忽略phantomjs中的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19459247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to ignore errors in phantomjs
提问by user2661048
I have a web crawler and I use phantomjs to parse pages, I want to get the html, but I always get this type of errors in the output before the html code
我有一个网络爬虫,我使用 phantomjs 来解析页面,我想获取 html,但是我总是在 html 代码之前的输出中收到此类错误
ReferenceError: Can't find variable: collapse_content_selector
http://staticloads.com/js/toggle.js?v=2013.10.04:135
TypeError: 'undefined' is not a function (evaluating '$('[placeholder]').placeholderLabel()')
how can I stop that
我怎么能阻止
回答by Cybermaxs
The easiest way is to add an error handler to phantom.onerroror webpage.onerror. These callbacks are invoked when there is a JavaScript execution error (in the page or in your script).
最简单的方法是向phantom.onerror或pages.onerror添加错误处理程序。当出现 JavaScript 执行错误(在页面或脚本中)时会调用这些回调。
page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
});
}
// uncomment to log into the console
// console.error(msgStack.join('\n'));
};