Alex F
Alex F

Reputation: 383

How to set a cookie in casperJS

I have a very simple casperjs script to visit several pages and take screenshots of some elements there. The problem is that after first load these pages show some overlay window that I definitely don't need on my screenshot. After overlay is displayed, it sets a cookie (prefs: {"dv":"1"}) which prevent this overlay from showing up on the next page load.

So my plan was to set this cooking via casperJS prior to loading the page. But for some reason it's not working and I'm not even sure I'm setting it correctly. Here are several options I've tried so far:

var casper = require('casper').create();

casper.start( url, function() {
document.cookie="prefs={\"dv\":\"1\"}";
this.captureSelector(filename + '.png', 'div#main.contentFrame');
});

casper.run();

or with

this.page.setCookies("prefs={\"dv\":\"1\"}")

and even

phantom.addCookie({
  'name': 'prefs',
  'value': {"dv":"1"},
  'domain': '.somesite.com'
});

What is the right way?

Upvotes: 2

Views: 3267

Answers (1)

sudipto
sudipto

Reputation: 2482

Probably, the page has not received the cookie.

You can try using the same code in load.started event listener.

Also, add a evaluate script to check the existence of the cookie on each stage of the page load. That might help.

Upvotes: 1

Related Questions