> I run :
> console.log('\n\n cookies we know about => \n\n' +
> JSON.stringify(phantom.cookies, null, 2));
> before page.open and have 4 cookies.
>
> The script continues and logs into the site and returns and I log the
> cookies again (this time I have 7 cookies including a ASP.NET_SessionId
> cookie.
> I am using the parameter --cookies-file=cookies.txt but it does not save the
> cookies to file (it seems to save them before page.open instead of at
> page.onLoadFinisThat is correct.hed)
--
--
--
I'd love to be of hand, but I think there is a fundamental thing to undertand here: SESSION COOKIES are supposed to DIE when you shutdown the browser (and they WON'T be saved).Also, that it's up to the webapp HOW they handle their cookies.
Thanks for your feedback!
For my case, I think the solution will be to supply the session ID as parameter to the script and use the API to manually set the cookie in code.
All in all, the current cookie implementation is much less usable than the pre 1.7 one for two reasons:
I think that both of these issues should be addressed in future releases.
--
--
Give it up! A big round of applause for Ivan and James!
--
I have nothing but admiration for the work that all you guys do. All the forum users (I am one of them) want to do is use your product. Everyone is at different levels of expertise and wanting to move forward but you guys are the only ones that can help us. Only recently have I started to read and post to forums and I see a lot of people ask questions without really attempting to do anything on their own first, like reading documentation and looking at examples. I have spent quite a bit of time trying to solve this on my own. I should have just kept to my problem without butting into AllOlli's comments.
I had already set this option and saved an image to verify that the "remember me" option was set.Maybe I am just doing the submit wrong and it is not actually logging in.
--
So, this is too difficult?
[...]
It's really THAT easy.
[General]
cookies=@Variant(\0\0\0\x7f\0\0\0\x16QList<QNetworkCookie>\0\0\0\0\x1\0\0\0\0)
[General]
cookies="@Variant(\0\0\0\x7f\0\0\0\x16QList<QNetworkCookie>\0\0\0\0\x1\0\0\0\x2\0\0\0OPHPSESSID=5db1uf7858n6gsg3q9kfhshgo4; domain=.abcdefg.a-domain-name.net; path=/\0\0\0HPHPSESSID=jmkmbvpear5p6722m7pag2p3c1; domain=other12.adomain.com; path=/)"
--
var g_cookiesFile = "selftest-cookies.txt";
function saveCookies() {
var fs = require("fs");
fs.write(g_cookiesFile, JSON.stringify(phantom.cookies));
console.log("Saving cookies: " + JSON.stringify(phantom.cookies));
}
function restoreCookies() {
console.log("Restoring cookies");
var fs = require("fs");
if (fs.exists(g_cookiesFile)) {
var cookies = fs.read(g_cookiesFile);
cookies = JSON.parse(cookies);
console.log("Restored cookies: " + JSON.stringify(cookies));
if (cookies.length > 0) {
for(var i=0; i<cookies.length; ++i) {
phantom.addCookie(cookies[i]);
console.log("add: " + JSON.stringify(cookies[i]));
}
}
//also tried: phantom.cookies = cookies;
}
}
restoreCookies();
console.log("Using cookies: " + JSON.stringify(phantom.cookies));
if (phantom.cookies.length === 0) {
phantom.addCookie({
'name': 'Valid-Cookie-Name',
'value': 'Valid-Cookie-Value',
'domain': 'localhost',
'path': '/',
'httponly': false,
'secure': false,
'expires': (new Date()).getTime() + 3600
});
saveCookies();
}
phantom.exit(0);expires/expiry fields.JSON.parse parses them as strings. And phantom fails on settings cookies with incorrect field (silently)
The simplest fix:
function restoreCookies() {
console.log("Restoring cookies");
var fs = require("fs");
if (fs.exists(g_cookiesFile)) {
var cookies = fs.read(g_cookiesFile);
cookies = JSON.parse(cookies);
if (cookies.length > 0) {
for(var i=0; i<cookies.length; ++i) {
var item = cookies[i];
if (item.expires)
item.expires = Date.parse(item.expires);
if (item.expiry)
item.expiry = Date.parse(item.expiry);
phantom.addCookie(item);
}
}
}
}
I tacked this question on to another thread, but I think it got lost in the weeds, so I want to answer it here with another quick summary.Run this script: https://gist.github.com/3808630$ phantomjs --cookies-file=cookies.txt test.jsThere are no cookies, so it forces a login, cookies are set, and persisted to cookies.txt. You can cat the file and see it is set.Now, run it again the same way:$ phantomjs --cookies-file=cookies.txt test.jsThis time, the script prints phantom.cookies array is populated, but it still forces a login, even if the user agent is spoofed, why?Thanks in advanceAdmittedly, I have not tried with any other server - but Reddit cookies definitely do work in my browser -- as a regular user, so I don't see why they shouldn't also work here.
web-security options (="no").
My cookie is httponly.