[Solved] Does PhantomJS support cookies?

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.

Enquirer: Niyaz


Solution #1:

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.

Respondent: Niyaz

Solution #2:

It does, through WebPage.addCookie() – which incidentally doesn’t work neither for me nor someone else.

You can use this instead:

    'name': 'mycookie',
    'value': 'something really important',
    'domain': 'example.com'
page.open('http://example.com/url/path/', function() {
Respondent: josh3736

Solution #3:

The work around I had to do was to execute javascript directly. I am using Geb and did the following:


When selenium fails I always fall back to javascript for functionality.

Respondent: johndodo

Solution #4:

I haven’t tried it yet, but doesn’t --cookies-file=/path/to/cookies.txt work?

It’s the first item in API reference…

Respondent: Derek Carr

Solution #5:

I had graded information within session recently. You should set cookie in page object, like below (coffeescript):

  'name'     : "JSESSIONID"
  'value'    : "0000rN3YAlVAU0xdHkKc6BEzWj9:-1"
  'domain'   : 'some.domain.com'
  'path'     : '/'
Respondent: Pooria Azimi

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.