简体   繁体   中英

How can I concisely check a webpage's resources for 404s using casperJS?

I have just started using Phantom/Casper.

So far I can list all the resources that a page has using this code:

casper.on('resource.received', function (resource) {
        casper.echo(resource.url);
   });

So far so good.

Now im trying to merge this with a chunk of code I gleaned and mashed from the documentation. I wanted to load each resource, and then print out the URL if it was missing:

casper.on('resource.received', function (resource) {
    //        casper.echo(resource.url);
    casper.Open(resource.url, function (resource) {
        this.on('http.status.404', function (resource) {
            this.echo('missing:' + resource.url);
        });
    });
});

It's messy, but it's what I've got. It fails to open the resources (ln 3), and the console shows no activity.

How can I rewrite this to iterate over the resources and check them for 404s?

(I am aware in my example that I'm not iterating over the resources, I was tempted to use eachthen(), but It's not clear if I can use general casperJS methods inside the 'test' prototype. Sorry, I hope this wasn't too long)

CasperJS' resource.received is based on PhantomJS' onResourceReceived . As you can see from the documentation, you can simply access resource.status . There is no need to explicitly load the resource.

casper.on('resource.received', function (resource) {
    if (resource.stage === "end" && resource.status === 404) {
        this.echo('missing:' + resource.url);
    };
});

Btw, you probably mean casper.open and not casper.Open .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM