node.jscookiesnpmnightmare

NightmareJS: how to set cookie?


var Nightmare = require('nightmare');
var nightmare = Nightmare({
    show: true
})

nightmare
    .goto('https://mail.yandex.ru')
    .type('input[name=login]', 'mylogin')
    .type('input[name=passwd]', 'mypassword')
    .click('button.nb-button._nb-action-button.nb-group-start')
    .wait('.mail-User-Name')
    .cookies.get()
    .then(function (cookies) {
        //actions
    })

I am getting cookies after authorization, but I don't know where I must set them and how I must set them. I've tried to do .cookie.set() at the beginning, but this doesn't work.

How I can use saved cookie? Thanks.


Solution

  • I did the following from the node terminal:

    > var Nightmare = require('nightmare')
    undefined
    > var nightmare = Nightmare({show:true})
    undefined
    > nightmare.
    ... goto('https://google.com').
    ... cookies.set('foo', 'bar').
    ... cookies.get().
    ... then((cookies) => {
    ...     console.log(JSON.stringify(cookies, null, 4))
    ... })
    Promise { <pending> }
    > [
        {
        "name": "NID",
        "value": "96=qo1qY9LTKh1np4OSgiyJTi7e79-_OIoIuc71hnrKWvN1JUnDLJqZlE8u2ij_4mW0-JJhWOCafo5J0j-YkZCFt8H2VHzYUom4cfEd2QLOEsHmAcT2ACx4a5xSvO0SZGZp",
        "domain": ".google.de",
        "hostOnly": false,
        "path": "/",
        "secure": false,
        "httpOnly": true,
        "session": false,
        "expirationDate": 1502733434.077271
        },
        {
        "name": "CONSENT",
        "value": "WP.25d07b",
        "domain": ".google.de",
        "hostOnly": false,
        "path": "/",
        "secure": false,
        "httpOnly": false,
        "session": false,
        "expirationDate": 2145916800.077329
        },
        {
        "name": "foo",
        "value": "bar",
        "domain": "www.google.de",
        "hostOnly": true,
        "path": "/",
        "secure": false,
        "httpOnly": false,
        "session": true
        }
    ]
    

    nightmare.cookies.set('key', 'value') is indeed the correct way to use it, as you can see in my result object. Perhaps https://mail.yandex.ru does not accept your cookie, because it's invalid. Please do the same and edit your question to include your results.

    Edit: Apparently, OP needs to store the cookies so he can use them in another Nightmare instance. This can be achieved like this:

    var Nightmare = require('nightmare')
    var storedCookies // This is where we will store the cookies. It could be stored in a file or database to make it permanent
    
    // First instance:
    var nightmare1 = Nightmare({show: true})
    nightmare1.
        goto('https://google.com').
        cookies.get().
        then((cookies) => {
            storedCookies = cookies
        })
    
    // Second instance:
    var nightmare2 = Nightmare({show: true})
    
    for(var i = 0; i < storedCookies.length; i++)
        nightmare2.
            cookies.set(storedCookies[i].name, storedCookies[i].value)
    
    nightmare2.
        goto('https://google.com')