知道为什么下面的代码在响应var或http.status.404事件中没有捕获404? 我用phantomjs 1.9,casperjs 1.0.2和Windows 7运行它 var casper = require("casper").create(), utils = require('utils');casper.start();casper.thenOpen
我用phantomjs 1.9,casperjs 1.0.2和Windows 7运行它
var casper = require("casper").create(),
utils = require('utils');
casper.start();
casper.thenOpen('http://www.google.com/sadfafsdgfsd', function(response) {
casper.capture('test.png');
utils.dump(response);
});
casper.on('http.status.404', function(resource) {
this.echo('wait, this url is 404: ' + resource.url);
});
casper.run(function() {
console.log('End');
casper.exit();
});
理想情况下,我喜欢在thenOpen()中捕获404.怎么做?
更新1:
我试过这个
casper.thenOpen('http://www.google.com/sadfafsdgfsd', function(response) {
casper.capture('test.png');
utils.dump(response);
if(this.status(false)['currentHTTPStatus'] === 404) {
console.log('Error 404');
} else {
console.log('No Error 404');
}
});
这是输出:
undefined No Error 404 End
它仍然没有意义.
更新2:
我在这里尝试了404checker.js https://gist.github.com/n1k0/4509789
casperjs 404.js http://www.google.com/sadfafsdgfsd
输出:
URI.js loaded Starting http://www.google.com/sadfafsdgfsd http://www.google.com/sadfafsdgfsd is okay (HTTP 200) 1 new links found on http://www.google.com/sadfafsdgfsd All done, 1 links checked.
发生什么了!?
我只是运行你的代码,它似乎工作正常捕获on事件中的404错误.如果你想在thneOpen()中捕获它,这样的东西可以工作:casper.thenOpen('http://www.google.com/sadfafsdgfsd', function() {
if(this.status(false)['currentHTTPStatus'] === 404) {
console.log('Error 404');
} else {
console.log('No Error 404');
}
});
或者你可以直接使用响应,在这种情况下,response [‘status’]将是404.
casper.thenOpen('http://www.google.com/sadfafsdgfsd', function(response) {
if(response['status'] === 404) {
console.log('Error 404');
} else {
console.log('No Error 404');
}
});
