PhantomJS - Cookie属性



我们对幻影对象和幻影网页对象都设置了cookie属性。使用cookie,您可以在URL上设置/获取cookie。它还将为您提供URL上的现有cookie和该页面上设置的新cookie。

语法

其语法如下 -

page.cookies;

示例

请看以下示例,了解如何使用cookies属性。

var wpage = require('webpage').create(); 
wpage.open('https:///tasks/a.html', function (status) { 
   var cookies = wpage.cookies;   
   console.log('Cookies available on page are as follows :'); 
   console.log(JSON.stringify(cookies));   
   phantom.exit(); 
});

上述程序生成了以下输出

Cookies available on page are as follows : 
[{"domain":"localhost","expires":"Fri, 22 Dec 2017 12:00:00 GMT","expiry":151394 
4000,"httponly":false,"name":"username","path":"/tasks/","secure":false,"value" : 
"Roy"}] 

如果您选中page.content示例,我们已经使用document.cookie = "username = Roy; expires = Thu, 22 Dec 2017 12:00:00 UTC";为该页面设置了cookie。

当我们尝试读取该页面的cookie时,它会列出cookie的所有详细信息,例如其Domain(域名)、Expires(过期时间)、Httponly、Name(名称)、Value(值)、Path(路径)等。page.cookies返回页面上所有可用的cookie。

phantomjs_webpage_module_properties.htm
广告
© . All rights reserved.