我刚开始使用CasperJs,而且我很难调试它,因为如此多的编码错误似乎会导致脚本退出而不提供错误消息。 当你使用详细模式时,你会得到你应该得到的消息,直到有问题的代码行,并且在那一刻它就会退出。
例如,如果我执行代码:
var casper = require('casper').create({ verbose: true, logLevel: "debug" }); casper.start('https://www.google.com/#q=stackoverflow', function(){ }); casper.wait(2000, function(){ }); casper.then(function() { hrefAr = this.evaluate(getLinks); this.log(hrefAr.length + ' links found', 'info'); this.exit(); }); casper.run(function() { this.exit(); }); function getLinks() { var links = document.querySelectorAll('a'); return Array.prototype.map.call(links, function(e) { return e.getAttribute('href'); }); }我得到以下结果:
...a bunch of info & debug messages, followed by... [info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&cad=h (HTTP 200) [info] [phantom] 89 links found [debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true [debug] [phantom] url changed to "about:blank"如果我向函数getLinks添加一条日志语句...
...code as shown above... function getLinks() { this.log('getLinks ran', 'info'); var links = document.querySelectorAll('a'); ...code as shown above......这会导致脚本失败,如下所示:
...the same info & debug messages... [info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&cad=h (HTTP 200) ...NO LOGS, ECHOS, OR RESULTS PAST THIS POINT, JUST THESE TWO CLOSING STATEMENTS... [debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true [debug] [phantom] url changed to "about:blank"它没有告诉你任何事情都出了问题,它只是让你回到空白并结束执行。
有没有办法获得更好的错误报告? 或者任何错误报告?
当我尝试使用以下代码实现下面的解决方法时:
var casper = require('casper').create({ verbose: true, logLevel: "debug" }); casper.start('https://www.google.com/#q=stackoverflow', function(){ }); casper.wait(2000, function(){ }); casper.then(function() { reportErrors(function() { hrefAr = this.evaluate(getLinks); this.log(hrefAr.length + ' links found', 'info'); this.exit(); }); }); casper.run(function() { this.exit(); }); function getLinks() { //this.log('getLinks ran', 'info'); var links = document.querySelectorAll('a'); return Array.prototype.map.call(links, function(e) { return e.getAttribute('href'); }); } function reportErrors(f) { var ret = null; try { ret = f(); } catch (e) { casper.echo("ERROR: " + e); casper.exit(); } return ret; }我得到...
...info & debug messages shown above... [info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&cad=h (HTTP 200) ERROR: TypeError: undefined is not a constructor (evaluating 'this.evaluate(getLinks)') --THIS IS WHERE MY LINK COUNT WOULD BE REPORTED [debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true [debug] [phantom] url changed to "about:blank"I just started working with CasperJs, and I'm having difficulty debugging it because so many coding mistakes seem to cause the script to exit without providing an error message. When you use verbose mode, you get the messages you should get up until the offending line of code, and at that point it just quits.
For example, if I execute the code:
var casper = require('casper').create({ verbose: true, logLevel: "debug" }); casper.start('https://www.google.com/#q=stackoverflow', function(){ }); casper.wait(2000, function(){ }); casper.then(function() { hrefAr = this.evaluate(getLinks); this.log(hrefAr.length + ' links found', 'info'); this.exit(); }); casper.run(function() { this.exit(); }); function getLinks() { var links = document.querySelectorAll('a'); return Array.prototype.map.call(links, function(e) { return e.getAttribute('href'); }); }I get the following results:
...a bunch of info & debug messages, followed by... [info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&cad=h (HTTP 200) [info] [phantom] 89 links found [debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true [debug] [phantom] url changed to "about:blank"If I add a log statement to the function getLinks...
...code as shown above... function getLinks() { this.log('getLinks ran', 'info'); var links = document.querySelectorAll('a'); ...code as shown above......this causes to script to fail, like so:
...the same info & debug messages... [info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&cad=h (HTTP 200) ...NO LOGS, ECHOS, OR RESULTS PAST THIS POINT, JUST THESE TWO CLOSING STATEMENTS... [debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true [debug] [phantom] url changed to "about:blank"It doesn't tell you that anything has gone wrong, it just sends you back to blank and ends execution.
Is there a way to get better error reporting? Or any error reporting?
When I try to implement the workaround below using the following code:
var casper = require('casper').create({ verbose: true, logLevel: "debug" }); casper.start('https://www.google.com/#q=stackoverflow', function(){ }); casper.wait(2000, function(){ }); casper.then(function() { reportErrors(function() { hrefAr = this.evaluate(getLinks); this.log(hrefAr.length + ' links found', 'info'); this.exit(); }); }); casper.run(function() { this.exit(); }); function getLinks() { //this.log('getLinks ran', 'info'); var links = document.querySelectorAll('a'); return Array.prototype.map.call(links, function(e) { return e.getAttribute('href'); }); } function reportErrors(f) { var ret = null; try { ret = f(); } catch (e) { casper.echo("ERROR: " + e); casper.exit(); } return ret; }I get...
...info & debug messages shown above... [info] [phantom] Step 4/4 https://www.google.com/search?q=stackoverflow&cad=h (HTTP 200) ERROR: TypeError: undefined is not a constructor (evaluating 'this.evaluate(getLinks)') --THIS IS WHERE MY LINK COUNT WOULD BE REPORTED [debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=true [debug] [phantom] url changed to "about:blank"最满意答案
这有一个开放的PhantomJS问题 。
你可以绕过它, then类似的reportErrors函数来解决它,例如:
function reportErrors(f) { var ret = null; try { ret = f(); } catch (e) { casper.echo("ERROR: " + e); casper.exit(); } return ret; } casper.then(function() { reportErrors(function() { ... }); });There is an open PhantomJS issue on this.
You can get around it by wrapping each then & similar with a reportErrors function, e.g.:
function reportErrors(f) { var ret = null; try { ret = f(); } catch (e) { casper.echo("ERROR: " + e); casper.exit(); } return ret; } casper.then(function() { reportErrors(function() { ... }); });更多推荐
发布评论