title: Can phantomjs read cookies set via javascript?
body: <p>I am trying to use phantomjs to automate the process of login into facebook. The issue is that facebook uses js codes to set cookies before the login form is submitted. Clicking on the email input box the login form can trigger the facebook js codes that set the cookies, so I created a script to click on the email box before login in, yet still I get the "Cookies are not enabled on your browser. Please enable cookies in your browser preferences to continue." message. Here is my code:</p>
  
  <pre><code>var page = require('webpage').create();
  var data = {};
  var fs = require('fs');
  var CookieJar = 'cookie.json';
  page.customHeaders = {
      'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0'
  };
  if (fs.isFile(CookieJar)) {
      Array.prototype.forEach.call(JSON.parse(fs.read(CookieJar)), function(x) {
          phantom.addCookie(x);
      });
  }
  page.open('https://www.facebook.com/login.php', function(status) {
      setTimeout(function() {
          pageloaded();
          setTimeout(function() {
              afterclick();
          }, 3000);
      }, 3000);
  
      function pageloaded() {
          pagex = page.evaluate(function() {
              function mouseclick(element) {
                  var event = document.createEvent(
                      'MouseEvents');
                  event.initMouseEvent('click', true,
                      true, window, 1, 0, 0);
                  element.dispatchEvent(event);
              }
              var element = document.querySelector('#email');
              mouseclick(element);
          });
      }
  
      function afterclick() {
          data.content = page.content;
          fs.write(CookieJar, JSON.stringify(phantom.cookies), 'w');
          console.log(page.content);
          phantom.exit();
      }
  });
  </code></pre>
  
  <p>This code basically goes to the facebook login page then clicks on the email input box. The end result should be that the cookie.json file contains the cookies facebook has set, but I don't get any cookies. I have also tried using the --cookies-file command line, seems to me that phantomjs can only store cookies set by the server side? Or am i doing something wrong?</p>