13

I think that every request that I send is being sent without cookies..

When I listen to the onResourceRequested event like this:

this.page.onResourceRequested = function(request) {
    utils.dump(request);
};

And every request has the same form of headers:

"headers": [
    {
        "name": "User-Agent",
        "value": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
    },
    {
        "name": "Accept",
        "value": "text/css,*/*;q=0.1"
    },
    {
        "name": "Referer",
        "value": "https://some_site.com/page"
    }
],

I never get a header with the cookies that are supposed to be there..

When I try to look at the cookies - I run this:

utils.dump(this.page.cookies);

I get a list of many cookies entries.

I think this giving me some errors on my scraping script.

your thoughts? thanks.

EDIT

I try to make a POST request to download a file. I can log into the site, browse to a few pages, get to the download page - but then, when I send the request I get a message "Error: To register with the The site you have to enable your browser to accept cookies."

This is why it is confusing - I can log in and browse the site ( so I must have some cookies passed around) but I can't download ( so I might not have a cookie here..)

Adam Vigneaux
  • 163
  • 3
  • 11
carmel
  • 902
  • 7
  • 24
  • What kind of requests are those that you make? Do you suspect that the cookies are actually not send or only not shown in those event handlers? Advice: don't use `casper.page.onResourceRequested`, but rather `casper.on("resource.requested", function(){...`, because you overwrite the event handler and CasperJS needs it internally to have a list or resources ready. – Artjom B. Jan 15 '15 at 15:59
  • I make GET and POST. "Do you suspect that the cookies are actually not send or only not shown in those event handlers" well.. I'm not sure.. by the look of the request output it looks like the cookies aren't there.. I was hoping that you could tell me :) Thanks for the advice, I'll use that. – carmel Jan 15 '15 at 18:42
  • actually.. I suspect the cookies aren't sent.. I'll edit the question – carmel Jan 15 '15 at 18:47
  • Are you able to share the site you're trying to work with? And could you add the options you're passing to Casper on startup? – mjk Mar 19 '16 at 14:30
  • Did you find a solution to this? I´m having exactly the same experience with cookies and casperjs. Using a cookie-file didn´t do anything for me. – perelin Apr 06 '16 at 05:47
  • @perelin To tell you the truth, I don't remember what happened with this one. I do remember that the cookie jar wasn't the answer. If I'll find it I'll post it. – carmel May 15 '16 at 07:18

2 Answers2

0

Provide a cookiejar when launching CasperJS to save cookies (and reuse them between sessions).

The command-line option:

--cookies-file=/path/to/cookies.txt

In full:

$ casperjs --cookies-file=/path/to/cookies.text myscript.js 
mjk
  • 2,443
  • 4
  • 33
  • 33
0

Use the command-line option:

casperjs --cookies-file=/path/cookies.text script.js 
ostapyrih
  • 79
  • 5