JavaScript Can phantomjs read cookies set via javascript? phantomjs read cookies

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:

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();
    }
});

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?

Answer:1

var j = jQuery.noConflict(); j.fn.portionHeight = function(ratio){ var width = j(this).width(); var height = Math.floor(width*ratio); j(this).height(height); } function initAndResize(){ ...

var j = jQuery.noConflict(); j.fn.portionHeight = function(ratio){ var width = j(this).width(); var height = Math.floor(width*ratio); j(this).height(height); } function initAndResize(){ ...

I was looking at this code example and stumbled upon this syntax: import React, { Component } from 'react'; const PrivateRoute = ({component:Component, ...rest}) => ( //(...) <Component {......

I was looking at this code example and stumbled upon this syntax: import React, { Component } from 'react'; const PrivateRoute = ({component:Component, ...rest}) => ( //(...) <Component {......

  1. this type annotation typescript
  2. this type annotation

I have an array like this: var arr = [ {a: 1, b: ['apple', 'banana', 'orange', 'mango'], c: [42, 7, 18, 5]}, {a: 2, b: ['apple', 'banana', 'orange', 'mango'], c: [4, 101, 88, 3]}, {a: 3, b: [...

I have an array like this: var arr = [ {a: 1, b: ['apple', 'banana', 'orange', 'mango'], c: [42, 7, 18, 5]}, {a: 2, b: ['apple', 'banana', 'orange', 'mango'], c: [4, 101, 88, 3]}, {a: 3, b: [...

  1. sort array from smallest to largest javascript
  2. sort array from smallest to largest c++
  3. sort array from smallest to largest java
  4. sort array from smallest to largest
  5. sort array from index
  6. sort array from object
  7. sort arrays in java
  8. sort arrays of 0 1 2
  9. sort arrays in python
  10. sort arrays in descending order java
  11. sort arrays in javascript
  12. sort arrays in c++
  13. sort arrays in php
  14. sort arrays in reverse order
  15. sort arrays in reverse order java
  16. sort arrays in descending order
  17. sort arrays in java 8
  18. sort arrays of string
  19. sort from array
  20. sort array of objects javascript

I am trying to search some words with special characters in jQuery Datatables plugin. There are some results in datatable like this: Peinado, Alma_María Aguilar Castillo, Antonio José When I try to ...

I am trying to search some words with special characters in jQuery Datatables plugin. There are some results in datatable like this: Peinado, Alma_María Aguilar Castillo, Antonio José When I try to ...

  1. jquery datatable not working
  2. jquery datatables responsive not working