Set cookie for request in CasperJS

I want to load a page using CapserJS, but how can I send a cookie that was exported from the chrome http request header on this page?

For instance:

"SUB = _2AkMjHt3gf8NhqwJRmPkQzG_qZIp_yA3EiebDAHzsJxJTHmMJ7IUyLkMN2K7WzRJvm-Tv3YY0xyZo; SUBP = 0033WrSXqPxfM72-Wf9jcj9jc1fcjf1fc2f1fc2fcfcfcfcfcfcfcfcfcfcfcfcdc

+6
source share
2 answers

There are several ways, but the easiest way is to use page.addCookie or phantom.addCookie that PhantomJS provides, but you will need to set the domain (and path). Keep in mind that page.addCookie must be executed on the loaded page, whereas phantom.addCookie may be executed earlier.

 var cookie = "someCookieName=Value; otherName=Value"; var domain = "example.com"; cookie.split(";").forEach(function(pair){ pair = pair.split("="); phantom.addCookie({ 'name': pair[0], 'value': pair[1], 'domain': domain }); }); casper.start("http://example.com", function(){ // check that cookie was indeed set: this.capture("screen.png"); }).run(); 
+6
source

You can try setting your cookie headers as follows:

 casper.start().thenOpen('http://yoururl', { headers:{ "Cookie" : "CookieName=cookieValue" } }, function() { // ... }); 
+1
source

Source: https://habr.com/ru/post/976929/


All Articles