- 文件系统模块
- PhantomJS - 属性
- PhantomJS - 方法
- 系统模块
- PhantomJS - 属性
- Web 服务器模块
- PhantomJS - 属性
- PhantomJS - 方法
- 其他
- 命令行界面
- PhantomJS - 屏幕截图
- PhantomJS - 页面自动化
- PhantomJS - 网络监控
- PhantomJS - 测试
- PhantomJS - REPL
- PhantomJS - 示例
- PhantomJS 有用资源
- PhantomJS - 快速指南
- PhantomJS - 有用资源
- PhantomJS - 讨论
PhantomJS - addCookie()
addCookie 方法将 cookie 添加到指定的页面。要添加 cookie,域名必须与页面匹配,否则会忽略 cookie。如果添加成功,则返回 true,否则返回 false。名称、值和**域**是 addcookie 方法中的强制字段。
现在,我们将 cookie 添加到页面 a.html。因此,wpage.cookies 将提供新添加的 cookie 和 a.html 页面上存在的现有 cookie。
语法
语法如下 −
phantom.addCookie({
'name' : 'cookie1', /* mandatory property */
'value' : '1234', /* mandatory property */
'domain' : 'localhost', /* mandatory property */
'path' : '/',
'httponly' : true,
'secure' : false,
'expires' : (new Date()).getTime() + (5000 * 60 * 60)
});
示例
我们来看一个 addCookie () 方法的示例。
var wpage = require('webpage').create();
phantom.addCookie ({
'name' : 'cookie1', /* mandatory property */
'value' : '1234', /* mandatory property */
'domain' : 'localhost', /* mandatory property */
'path' : '/',
'httponly' : true,
'secure' : false,
'expires' : (new Date()).getTime() + (5000 * 60 * 60)
});
wpage.open ('https:///tasks/a.html', function() {
console.log(JSON.stringify(wpage.cookies));
phantom.exit();
});
上述程序生成以下输出。
[{"domain":".localhost","expires":"Sun, 07 May 2017 01:13:45 GMT","expiry":1494
99825,"httponly":true,"name":"cookie1","path":"/","secure":false,"value":"1234"
,{"domain":"localhost","expires":"Fri, 22 Dec 2017 12:00:00 GMT","expiry":15139
4000,"httponly":false,"name":"username","path":"/tasks/","secure":false,"value"
"Roy"}]
phantomjs_webpage_module_methods.htm
广告