可以't从JSON在Phantomjs中添加cookie
Can't add cookies in Phantomjs from JSON
我试图使用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);
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- ZeroClipboard-在复制之前添加到值
- 我可以在json对象中添加一个函数吗
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何添加浮动和非浮动,其他
- 添加文字和评论功能更新Div
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 在函数中添加数组元素的数值
- 如何将歌曲添加到jPlayer
- 在html Select中添加搜索
- Java脚本时间添加
- 可以't从JSON在Phantomjs中添加cookie
- 将脚本添加到 DOM 后,JQuery 不能在 Watir 中使用 PhantomJS
- 将jQuery选择器控件添加到phantomJS无头浏览器中
- 如何在Phantomjs Webserver中添加特定路由
- 当添加回调函数时,phantomJS的click()不起作用