2013-03-30 16 views
3

我正在嘗試查找dom中的資源,以便我跟蹤當前頁面的每個鏈接。 問題是,第一次完美地工作,但當我開始按照我收到的鏈接「餅乾未啓用」。 我這樣調用腳本:casperjs --cookies文件=的/ tmp/mycookies.txt的script.js 下面是腳本:Casperjs - Cookies未啓用後再打開

var casper = require('casper').create({ 
    logLevel: "debug",    // Only "info" level messages will be logged 
    verbose:true, 
    onError: function(self, m) { // Any "error" level message will be written 
     console.log('FATAL:' + m); // on the console output and PhantomJS will 
     self.exit();    // terminate 
    }, 
    pageSettings: { 
     javascriptEnabled: true, 
     loadImages: false,  // The WebPage instance used by Casper will 
     loadPlugins: false,   // use these settings 
     userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4' 
    } 
}); 
var fs = require('fs'); 
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'); 
phantom.cookiesEnabled = true; 
var x = require('casper').selectXPath; 

function getLinks() { 
    var links = document.querySelectorAll('a'); 
    return Array.prototype.map.call(links, function(e) { 
     return e.getAttribute('href') 
    }); 
} 

function followLinks(links) { 
    casper.each(links, function(self, link) { 
      self.thenOpen(link, function() { 
       fs.write("pages/" + link+ ".html", this.getHTML(), 'w'); //for debugging 
       if (!lookFor(x('//input[@name="Export"]'))) { 
        var links = getLinks(); 
        followLinks(links); 
       } 
     }); 
    }); 
} 
casper.start('http://www.url.com', function() { 
    this.fill('form#formAuth', { 
     login: '[email protected]', 
     password: 'apass' 
    }, true); 
}); 

casper.then(function() { 
    links = links.concat(this.evaluate(getLinks)); 
    followLinks(links); 
}); 

casper.run(function() { 
    this.exit(); 
}); 
+0

重要的是要確保您發佈前檢查錯別字! OpenOpen不是CasperJS庫中的函數。 :) – thealexbaron

+0

謝謝,修復它;) –

回答

1

的問題是其中一個鏈接,是註銷鏈接: D.這就是爲什麼突然我得到錯誤