Casperjs - 在打开之后不启用Cookies

时间:2013-03-30 11:39:56

标签: cookies casperjs

我正在尝试在dom中查找资源,以便我跟踪当前页面的每个链接。 问题是第一次运行得很好,但是当我开始关注链接时,我收到“cookie未启用”。 我这样调用脚本:casperjs --cookies-file = / tmp / mycookies.txt script.js 这是脚本:

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: 'user@url.com', 
        password:  'apass'
    }, true);
});

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

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

1 个答案:

答案 0 :(得分:1)

问题是其中一个链接是Logout Link:D。这就是我突然得到错误

的原因