12

I want my casper to log in and stay logged in during the session. Is it possible? If so, how?

Euphe
  • 3,531
  • 6
  • 39
  • 69

2 Answers2

21

If you want to store cookies in CasperJS, you can use the PhantomJS cookiejar, which is accesible natively through CasperJS.

Simply add following option when you launch CasperJS:

--cookies-file=cookies.txt

Your cookies will be stored in that file. Even after exiting CasperJS, if you call CasperJS with this option, you'll have access to those cookies again.

Hope that helps.

Thomas
  • 2,070
  • 3
  • 16
  • 21
  • The cookies don't get written into the file. – Euphe Jan 07 '14 at 11:06
  • 1
    If you launch CasperJS with the option like this: "casperjs test --cookies-file=cookies.txt scraper.js" it should definetly work. What version of PhantomJS are you using? – Thomas Jan 08 '14 at 10:59
3

What do you mean? are you just sitting idle in session? Typically you do not want casper.exit() to be called. So if there are times you want to wait a bit but stay logged in you should define that time in a

casper.wait(60000, function () {
});
Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68
  • Let's say casper submitted a login form, logged in. If casper navigates to a different page, does it stay logged in? If I restart casper, will it stay logged in after reconnecting? – Euphe Jan 03 '14 at 16:37
  • 1
    No if you restart CasperJS it will kill your existing session and cache. – Chris Hawkes Jan 03 '14 at 18:07