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 remember authorization when parsing a site on nodejs cookies #1632

Open
dvijnru opened this issue Apr 25, 2021 · 0 comments
Open

How to remember authorization when parsing a site on nodejs cookies #1632

dvijnru opened this issue Apr 25, 2021 · 0 comments

Comments

@dvijnru
Copy link

dvijnru commented Apr 25, 2021

Hello, i go to the site and look for an authorization form, if I find it, then I log in, if not, then I do the actions I need

let cookie_ = fs.readFileSync("cookies.json");//I am looking for a file with saved cookies
cookie = JSON.parse(cookie_);//Converting to json

nightmare
.goto('https://site.ru/login')//I go to the site
.cookies.set(cookie)//I substitute cookies from the file
.evaluate(function () {
    return document.querySelector('input[id="email"]');//I am looking for a field to enter mail
})
.then(function (page) {
    if(page) {//I check if there is a field for entering mail
        f().then(function (cookies) {//We get the result from the function
            require('fs').writeFileSync(//And write to file
                'cookies.json',
                JSON.stringify(cookies)
            );
        })

    } else {
        console.log('You are logged in');
    }
})
async function f() {//I call the function if we are not authorized
        return new Promise((resolve, reject) => {
            nightmare
                .goto('https://site.ru/login')
                .type('input[id="email"]', 'login')//Enter mail
                .type('input[id="password"]', 'passord')//Enter your password
                .click('.btn.btn-danger')//Click on the authorization button
                .wait(2000)//We wait 2 seconds
                .cookies.get()//We receive cookies
                .then(resolve)

        });
    }

The file is created, cookies are written, but with the next attempts to run the script, the authorization form still appears

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

No branches or pull requests

1 participant