Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add cookie? #118

Open
herlon214 opened this issue Jun 30, 2015 · 3 comments
Open

How to add cookie? #118

herlon214 opened this issue Jun 30, 2015 · 3 comments

Comments

@herlon214
Copy link

My code:

var ph = require('phantom');

ph.create(function(err, phantom) {
  console.log('Loading a web page');
  phantom.addCookie({
    "name": "PHPSESSID",
    "value": "9lrinitbsot9lvf7d7peej1456",
    'domain': '.google.com'
  });

  phantom.createPage(function(page) {
    var url = 'http://google.com';
    page.open(url, function (status) {
      page.render('image.png');
      // var link = page.evaluate(function() {
      //   return document.getElementById("download").getAttribute('href');
      // });
    });
  });

},{
  dnodeOpts: {
    weak: false
  }
});

It's returning:

c:\Users\herlon214\dev\nodejs\foo\generatorjs\index.js:5
  phantom.addCookie({
         ^
TypeError: Cannot read property 'addCookie' of undefined
    at ph.create.dnodeOpts.weak (c:\Users\herlon214\dev\nodejs\foo\gener
atorjs\index.js:5:10)
    at D.<anonymous> (c:\Users\herlon214\dev\nodejs\foo\generatorjs\node
_modules\phantom\phantom.js:164:45)
    at D.emit (events.js:110:17)
    at Proto.<anonymous> (c:\Users\herlon214\dev\nodejs\foo\generatorjs\
node_modules\phantom\node_modules\dnode\lib\dnode.js:53:14)
    at Proto.emit (events.js:107:17)
    at Proto.handleMethods (c:\Users\herlon214\dev\nodejs\foo\generatorj
s\node_modules\phantom\node_modules\dnode\node_modules\dnode-protocol\index.js:1
18:10)
    at Proto.handle (c:\Users\herlon214\dev\nodejs\foo\generatorjs\node_
modules\phantom\node_modules\dnode\node_modules\dnode-protocol\index.js:77:14)
    at D.dnode.handle (c:\Users\herlon214\dev\nodejs\foo\generatorjs\nod
e_modules\phantom\node_modules\dnode\lib\dnode.js:140:21)
    at D.dnode.write (c:\Users\herlon214\dev\nodejs\foo\generatorjs\node
_modules\phantom\node_modules\dnode\lib\dnode.js:128:22)
    at SockJSConnection.ondata (stream.js:51:26)
@shroukkhan
Copy link

did you figure out how to add a cookie ?

@SgtPooki
Copy link

This issue should be clearly defined in some documentation somewhere because I have been trying for days to figure out how to successfully set cookies with phantomjs directly (script call from phantomjs), node-phantom, and phantomjs-node. It doesn't seem like any of the phantomjs-to-node modules are actively maintained at all.

@shroukkhan
Copy link

@SgtPooki i figured it out later. indeed there should be more examples / documentation . fyi, it is ph.addCookie

example:

/**
 * Created by KHAN on 1/28/2016.
 */

phantom.create(function (error, ph) {

    ph.createPage(function (err, page) {


        page.set('settings', {
            userAgent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:40.0) Gecko/20100101 Firefox/40.0",
            javascriptEnabled: true,
            loadImages: false
        });

        phantom.cookiesEnabled = true;
        phantom.javascriptEnabled = true;


        steps = [

            function () {
                log.debug(site + 'Step 1 - Open login page : ' + loginPage);
                page.open(loginPage, function (status) {

                });
            },


            function () {
                log.debug(site + 'Step 2 - Populate and submit the login form');
                page.render('screenshots/xxxx/step-1-login-page.png');
                var onLoginComplete = function (error, response, body, page, cookies) {

                    for (var i = 0; i < cookies.length; i++) {

                        if (cookies[i].key === authCookieName) {

                            log.info("Logged into xxxx , " + cookies[i].key + " is \n" + cookies[i].value);

                            ph.addCookie({
                                domain: 'xxxx.com',
                                expires: (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */,
                                httponly: true,
                                name: authCookieName,
                                path: '/',
                                secure: false,
                                value: cookies[i].value
                            });


                        }
                    }

                    loadInProgress = false;

                };


                page.get('cookies', function (err, cookies) {


                    for (var i = 0; i < cookies.length; i++) {

                        if (cookies[i].name === sessionCookieName && cookies[i].value.length > 0) {
                            loadInProgress = true;
                            myself.login(username, password, cookies[i].value, ph, onLoginComplete);

                        }
                    }


                });


            },
        ]

    })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants