Leo Jiang
Leo Jiang

Reputation: 26085

PhantomJS isn't saving cookies

I'm running the latest version of PhantomJS on Windows 8.1. Here's my code:

console.log('cookies: ',phantom.cookies);

phantom.addCookie({
    domain:'google.com',
    path:'/',
    expires:(new Date()).getTime() + (1000 * 60 * 60),
    name:'test',
    value:'value'
});

phantom.exit();

And I tried calling it like this:

phantomjs test.js --cookies-file=cookies.txt

and

phantomjs test.js --cookies-file="C:\path\cookies.txt"

Either way, cookies.txt isn't being created and phantom.cookies is empty. How do I make PhantomJS save cookies?

Upvotes: 0

Views: 1311

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

--cookies-file is supposed to be an option for PhantomJS and not for your script. PhantomJS also supports passing commandline arguments to the script. You need to flip the order:

phantomjs --cookies-file=cookies.txt test.js

When you execute this line once, phantom.cookies will be empty, but cookies.txt will be created with a single entry. Only when you execute it a second time you will see that phantom.cookies isn't empty.

The cookie jar is read before your script starts and written after you exit your script.

Upvotes: 2

Related Questions