How to follow all links in CasperJS?

前端 未结 2 776
长发绾君心
长发绾君心 2020-12-10 08:21

I\'m having trouble clicking all JavaScript based links in a DOM and saving the output. The links have the form



        
2条回答
  •  情书的邮戳
    2020-12-10 08:49

    I have this script that first will get all links from a page then save 'href' attributes to an array, then will iterate over this array and then open each link one by one and echo the url :

    var casper = require('casper').create({
        logLevel:"verbose",
        debug:true
    });
    var links;
    
    casper.start('http://localhost:8000');
    
    casper.then(function getLinks(){
         links = this.evaluate(function(){
            var links = document.getElementsByTagName('a');
            links = Array.prototype.map.call(links,function(link){
                return link.getAttribute('href');
            });
            return links;
        });
    });
    casper.then(function(){
        this.each(links,function(self,link){
            self.thenOpen(link,function(a){
                this.echo(this.getCurrentUrl());
            });
        });
    });
    casper.run(function(){
        this.exit();
    });
    

提交回复
热议问题