0

Im trying to store cookies after I get from website.But, when I create a global variable and I try to store it on the variable it return as undefined or when I try to create a file with the cookies on it, it return undefined as well, but I can print it with console.log.

My code:

var cookies;

phantom.create(['--ignore-ssl-errors=yes', '--load-images=no', '--ssl-
protocol=any']).then(function(ph) {
ph.createPage().then(function(page) {

page.property("onConsoleMessage", function(message) {
   console.log(message); 
});

page.property('onResourceReceived', function(response) {
        console.log(phantom.cookies); //Show the page cookies
        cookies = phantom.cookies;
       fs.write(CookieJar, JSON.stringify(phantom.cookies), "w"); //its not creating a file
}).then(function() {
      console.log('Cookies');
      console.log(phantom.cookies); //Equals to undefined
      console.log(cookies); //Equals to undefined
});;

page.open('https://stackoverflow.com').then(function(status) {
  if(status == 'success') {
     console.log('success');
  }  
});

});
});

How can I store those cookies on a file and get it?

Tiago Castro
  • 361
  • 2
  • 15

1 Answers1

0

You can look at the tests for possible use cases. Here is an example:

await page.addCookie({
  name: 'Valid-Cookie-Name',
  value: 'Valid-Cookie-Value',
  domain: 'localhost',
  path: '/foo',
  httponly: true,
  secure: false,
  expires: new Date().getTime() + (1000 * 60 * 60),
});
Amir Raminfar
  • 32,592
  • 7
  • 83
  • 119