PhantomJS是否支持Cookie? [英] Does PhantomJS support cookies?

查看:577
本文介绍了PhantomJS是否支持Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PhantomJS 是否支持Cookie?如果是的话,我在哪里可以找到API详细信息?



我现在无法找到它。

解决方案

是,自 1.7 Phantom已完成Cookie处理,默认情况下已启用。



如果您希望在运行Phantom时保留Cookie,则会有command-line选项 cookies-file ,您可以在其中指定存储持久性Cookie的位置。 / p>


- cookies-file = / path / to / cookies.txt


在页面脚本中,您可以使用常规的 document.cookie 属性。像浏览器一样,此属性返回与 Cookie: HTTP标头中发送的字符串类似的字符串。



在Phantom脚本中,您可以通过 page.cookies 访问Cookie的页面(受到一般来源限制),返回对象。 / p>

您还可以使用 phantom.cookies 访问所有域中的所有

  var page = require('webpage')。 
page.open('http://example.com',function(status){
page.evaluate(function(){
document.cookie; // =>test = test-value;
});
page.cookies; // => [{
// domain:example.com,
// expires: Wed,08 Jan 2014 00:00:00 GMT
// httponly:false,
//名称:test,
//路径:/,
// secure:false,
// value:test-value
//}]
phantom.cookies; //包含Phantom的jar中的所有cookie
} ;

添加/编辑/删除Cookie ,使用 addCookie deleteCookie ,以及 WebPage 对象或幻像 clearCookies 方法>对象。



当您使用 WebPage 对象的方法时,这一页。



但是,使用 phantom 的cookie方法可以访问全部 Cookie。 phantom.addCookie 需要一个域( WebPage.addCookie 假定为当前域,如果不指定) phantom.deleteCookie 删除与指定名称相符的任何 Cookie。


Does PhantomJS support cookies? If yes, where can I find the API details?

I am not able to figure it out after searching for a while now.

解决方案

Yes, as of 1.7 Phantom has complete cookie handling, enabled by default. Cookies are retained for the duration of the process's life.

If you'd like to retain cookies across runs of Phantom, there's a command-line option cookies-file where you can specify where to store persistent cookies.

--cookies-file=/path/to/cookies.txt specifies the file name to store the persistent cookies.

In page script, you can use the regular document.cookie property. Like in browsers, this property returns a string similar to that which would be sent in the Cookie: HTTP header.

In Phantom script, you can access cookies for a page (subject to the usual origin restrictions) via page.cookies, which returns objects.

You can also access all cookies (from all domains) using phantom.cookies.

var page = require('webpage').create();
page.open('http://example.com', function (status) {
    page.evaluate(function() {
        document.cookie; // => "test=test-value;"
    });
    page.cookies; // => [{
                  //   domain: "example.com",
                  //   expires: "Wed, 08 Jan 2014 00:00:00 GMT"
                  //   httponly: false,
                  //   name: "test",
                  //   path: "/",
                  //   secure: false,
                  //   value: "test-value"
                  // }]
    phantom.cookies; // contains ALL cookies in Phantom's jar
});

To add/edit/delete cookies, use the addCookie, deleteCookie, and clearCookies methods of either a WebPage object or the phantom object.

When you use the methods of a WebPage object, you only modify the cookies that are visible to the page. Access to other domains is blocked.

However, using phantom's cookie methods allow access to all cookies. phantom.addCookie requires a domain (WebPage.addCookie assumes the current domain if you don't specify one), and phantom.deleteCookie deletes any cookie matching the specified name.

这篇关于PhantomJS是否支持Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆