Cookies phantom.addCookie不';行不通

Cookies phantom.addCookie不';行不通,cookies,phantomjs,Cookies,Phantomjs,我举了一个例子: /* global phantom:false */ var page = require('webpage').create(); var cookies = require('./cookie'); for (var i=0; i<cookies.length; i++) { (function(item){ console.log('Add cookie:', item.name + '=' + item.value); phantom.ad

我举了一个例子:

/* global phantom:false */

var page = require('webpage').create();
var cookies = require('./cookie');

for (var i=0; i<cookies.length; i++) {
  (function(item){
    console.log('Add cookie:', item.name + '=' + item.value);
    phantom.addCookie({
      name: item.name,
      value: item.value,
      domain: item.domain,
      path: item.path,
      httponly: item.httpOnly,
      secure: item.secure,
      expires: item.expirationDate
    });
  })(cookies[i]);
}


page.open('http://www.html-kit.com/tools/cookietester/', function() {
  page.render('example.png');
  phantom.exit();
});
不知什么原因,它对我不起作用:


对于phantomJS 1.9.2,cookie的工作原理如下:

var page = require('webpage').create();

phantom.addCookie({
  'name'     : 'TestCookie_Name_201312174009',   /* required property */
  'value'    : 'TestCookie_Value_164009',  /* required property */
  'domain'   : 'www.html-kit.com',        /* required property */
  'path'     : '/',
  'httponly' : true,
  'secure'   : false,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */
});


page.open('http://www.html-kit.com/tools/cookietester/', function() {
    //console.log(page.plainText);
    page.render('example.png');
    for (var i = 0; i < page.cookies.length; i++) {
        console.log(page.cookies[i].name + "=" + page.cookies[i].value);
    }
    phantom.exit();
});
请注意,cookie名称和值应该有特殊的模式,否则html工具包看起来不接受它


如果要保存cookie以供以后使用,可以添加--cookies文件选项

如何添加多个cookie?
var page = require('webpage').create();

phantom.addCookie({
  'name'     : 'TestCookie_Name_201312174009',   /* required property */
  'value'    : 'TestCookie_Value_164009',  /* required property */
  'domain'   : 'www.html-kit.com',        /* required property */
  'path'     : '/',
  'httponly' : true,
  'secure'   : false,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */
});


page.open('http://www.html-kit.com/tools/cookietester/', function() {
    //console.log(page.plainText);
    page.render('example.png');
    for (var i = 0; i < page.cookies.length; i++) {
        console.log(page.cookies[i].name + "=" + page.cookies[i].value);
    }
    phantom.exit();
});
phantomjs cookie.js