35

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.

Niyaz
  • 53,943
  • 55
  • 151
  • 182
  • yes it supports cookie like browsers, what do you mean by API? – Anurag Uniyal Feb 29 '12 at 18:47
  • How do I get the value of a cookie? document.cookie does not seems to get anything at all. – Niyaz Feb 29 '12 at 18:48
  • 1
    document.cookie works in all browsers except in phontomjs, seems to be some bug, there are plans to add native API http://code.google.com/p/phantomjs/issues/detail?id=354&start=100 – Anurag Uniyal Feb 29 '12 at 18:56

5 Answers5

45

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.

josh3736
  • 139,160
  • 33
  • 216
  • 263
13

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

You can use this instead:

phantom.addCookie({
    'name': 'mycookie',
    'value': 'something really important',
    'domain': 'example.com'
})
page.open('http://example.com/url/path/', function() {
    console.log(page.cookies);
})
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
johndodo
  • 17,247
  • 15
  • 96
  • 113
  • Why the (anonymous) downvote? This snippet shows another way to set cookies if the `WebPage.addCookie()` fails to work for some reason. – johndodo Dec 02 '13 at 08:53
  • I cannot find the addCookie method in my node modules phantomjs-prebuilt, How do I get to it? – EugenSunic Apr 13 '18 at 13:51
  • Sorry, can't help you with that - I suggest you ask another question and someone else might know the answer. – johndodo Apr 14 '18 at 19:36
  • but How were you able to reference the method which you are using for your answer? – EugenSunic Apr 14 '18 at 19:53
  • It exists, follow the link in the answer: http://phantomjs.org/api/webpage/method/add-cookie.html Unfortunately I don't have enough information about your problem to help you, and I haven't touched PhantomJS in years anyway... Sorry. You really need to ask a separate question if you need help. – johndodo Apr 16 '18 at 12:12
  • It does indeed but in my 2+ version I cannot see it in my node_modules, very interesting... – EugenSunic Apr 16 '18 at 12:27
1

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

js.exec("document.cookie='PHPSESSID=${cookie}';")

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

Derek Carr
  • 73
  • 1
  • 7
0

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

@page.clearCookies()
@page.addCookie
  'name'     : "JSESSIONID"
  'value'    : "0000rN3YAlVAU0xdHkKc6BEzWj9:-1"
  'domain'   : 'some.domain.com'
  'path'     : '/'
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
jiahut
  • 1,451
  • 15
  • 14
0

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

It's the first item in API reference...

Pooria Azimi
  • 8,585
  • 5
  • 30
  • 41
  • 1
    Yes, it writes the cookies to the specified file. but how do I get back the vale of a specific cookie? – Niyaz Feb 29 '12 at 18:51
  • 1
    yes... it seems that cookie support is not complete: http://code.google.com/p/phantomjs/issues/detail?id=299 and http://code.google.com/p/phantomjs/issues/detail?id=91 – Pooria Azimi Feb 29 '12 at 19:00