Keeping cookies

In PhantomJS, cookies are not persistent by default. All cookies and cookie manipulation in our script will be destroyed and gone after the script execution. Each time we call our script, it will recreate the cookies again as if we had not visited the page before. If we need to persist with our cookies (like browsers do), we need to specify a cookie file. This cookie file will save all cookies from the different sites and domains we visit into a text file. The cookie file is passed in via the PhantomJS command line:

phantomjs –cookies-file=~/phantomjs/cookies.txt cookies1.js

If the cookie file does not exist yet, PhantomJS will create one; if it does exist, then it will load the cookies from the file we specified.

Get Getting Started with PhantomJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.