可以't从JSON在Phantomjs中添加cookie

Can't add cookies in Phantomjs from JSON

本文关键字:Phantomjs 添加 cookie JSON 可以      更新时间:2024-05-21

我试图使用PhantomJS动态加载一些cookie,但遇到了一些错误。这是我的代码:

var page = require('webpage').create();
var cookieJson = require('cookie.json'); // local cookie file
phantom.cookiesEnabled = true; // Enable Cookies
phantom.clearCookies(); // Clear Old Cookies
for(var i = 0; i< cookieJson.length; i++) { //for each domain, try to add the cookie
    var temp = cookieJson[i];
    console.log(JSON.stringify(temp));  // This seems to print just fine
    phantom.addCookie(temp); // this throws an exception
    }

phantom.exit();

上述代码引发以下异常:

incompatible type of argument(s) in call to addCookie(); candidates were
    addCookie(QVariantMap)

我相信这里有一个简单的解决方案,但我的大脑冻结了。我的印象是JSON.stringify从JSON对象返回一个字符串。真正令人困惑的是,当我将其打印到控制台时,它看起来和我将其存储为String时完全一样。我的数据如下:

{"domain": ".sweatytacobo.com",
"expires": "Tue, 10 Jun 2014 16:37:46 GMT",
"expiry": ,
"httponly": false,
"name": "__utmz",
"path": "/",
"secure": false,
"value": "268881515.13222266.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"}

当我把上面的作为字符串使用时,它会毫无问题地添加。那么,为什么我的JSON.Stringify会给我带来问题呢?

编辑:

根据PhantomJS源代码的评论,addCookie被传递了一个格式为:的QVariantMap

 {
     *   "name"     : "cookie name (string)",
     *   "value"    : "cookie value (string)",
     *   "domain"   : "cookie domain (string)",
     *   "path"     : "cookie path (string, optional)",
     *   "httponly" : "http only cookie (boolean, optional)",
     *   "secure"   : "secure cookie (boolean, optional)",
     *   "expires"  : "expiration date (string, GMT format, optional)"
     * }

那么,我不应该以某种方式传递一个JSON对象吗?

好吧,我想明白了。PhantomJS addCookie对cookie的格式非常挑剔。

基本上,为了转换JSON,您必须通过迭代JSON对象来获取值。例如:

var cookieJson = require('cookiefile.json'); 
// load in the cookies in JSON format from file
for(var i = 0; i< cookieJson.length; i++) {
    var tempCookie = {}; 
    tempCookie["name"] = cookieJson[i]["name"];
    tempCookie["domain"] = cookieJson[i]["domain"];
    tempCookie["value"] = cookieJson[i]["value"];
    tempCookie["path"] = cookieJson[i]["path"];
    tempCookie["session"] = cookieJson[i]["session"];
    tempCookie["storeId"] = cookieJson[i]["storeId"];
    // Here we are adding the relevant values as needed and in the proper format
    var tempADD = {"domain": tempCookie["domain"],
    "expires": tempCookie["expires"],
    "expirationDate": 1402418266,
    "httpOnly": tempCookie["httpOnly"],
    "name": tempCookie["name"],
    "path": tempCookie["path"],
    "secure": tempCookie["secure"],
    "value": tempCookie["value"],
    "session": tempCookie["session"],
    "storeId": tempCookie["storeId"]};
    // Finally, we add the cookie. phantom.addCookie returns true if successful
    if(phantom.addCookie(tempADD)){
        console.log("Cookie Added!");
        } else {
        console.log("Cookie Failure: ");
        console.log(JSON.stringify(tempADD)) // print the cookie which failed to load
        };
    }

您必须实际将对象的字符串化版本分配回变量:

temp = JSON.stringify(temp);