如何在caperJS代码中解决此“空不是对象错误”

时间:2019-04-30 07:11:11

标签: javascript casperjs

我正在添加这部分代码以使用cookie值进行自动说明,并且在迭代时遇到错误,请问您有什么想法吗?

casper
    .start()
    .thenOpen(url, function() {
        this.evaluate(function() {
            document.cookie = "MplusUserSettings=Lng=1; expires=Fri, 1 Aug 2100 20:00:00 UTC; path=/";
            document.cookie = "variable1=value1; 
            document.cookie = "variable2=value2; 
            document.cookie = "variable3=value3; 
        });
    })
    .thenOpen(url, function() {
        var links = this.evaluate(function() {
            return Array.prototype.map.call(document.querySelectorAll('ul#Root a'), function(link){
                return link.getAttribute('href');
            });
        });
        this.each(links, function(self, link) {
            var id = /ida=(\d+)/g.exec(link)[1]; // problem in this line
            // May be useful to continue from last id
            //if (Number(id) > 000000) {
                self.thenOpen(resolver(link, url), function() {
                        self.download("http://www.link.com/file.pdf);
                });
            //}
        });
    })
    .run(function(){
        this.exit();
    });

我收到此错误:

TypeError: null is not an object (evaluating '/ida=(\d+)/g.exec(link)[1]')      
      /Users/soufianeelhidaoui/Downloads/ENIDownloader-master/phantomjs:/code/app.js:41
      /Users/soufianeelhidaoui/Downloads/ENIDownloader-master/phantomjs:/platform/casper.js:719 in _forEach
      /Users/soufianeelhidaoui/Downloads/ENIDownloader-master:0 in forEach
      /Users/soufianeelhidaoui/Downloads/ENIDownloader-master/phantomjs:/platform/casper.js:718 in each
      /Users/soufianeelhidaoui/Downloads/ENIDownloader-master/phantomjs:/code/app.js:40
      /Users/soufianeelhidaoui/Downloads/ENIDownloader-master/phantomjs:/platform/casper.js:1728 in runStep
      /Users/soufianeelhidaoui/Downloads/ENIDownloader-master/phantomjs:/platform/casper.js:453 in checkStep
    res.err: {"errorCode":5,"errorString":"Operation canceled","id":55,"status":null,"statusText":null,"url":"http://www.eni-training.com/client_net/get_Resource.aspx"}

0 个答案:

没有答案