Git Product home page Git Product logo

Comments (3)

shroukkhan avatar shroukkhan commented on August 28, 2024

did you figure out how to add a cookie ?

from node-phantom.

SgtPooki avatar SgtPooki commented on August 28, 2024

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.

from node-phantom.

shroukkhan avatar shroukkhan commented on August 28, 2024

@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);

                        }
                    }


                });


            },
        ]

    })
})

from node-phantom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.