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

Opening page after setting cookies from previously saved cookies does not work (not logged in) #717

Closed
xprudhomme opened this issue Sep 8, 2017 · 12 comments
Assignees

Comments

@xprudhomme
Copy link
Contributor

xprudhomme commented Sep 8, 2017

** Environment:**

  • Puppeteer version: v0.10.1
  • Node version: v8.4.0
  • Platform / OS version: Ubuntu 14.04
  • Url: linkedin.com

** Steps to reproduce?**
First step:

  • Open a new browser
  • Open home page (we are logged out at this stage)
  • Fill in login details (email, password)
  • Click on the log-in button (submit)
  • Get and save cookies
    (Everything works fine up to here, we are logged-in and the cookies are valid, and well saved, I've checked).

Second step:

  • Open a new browser
  • Inject/set cookies with values from previously saved cookies
  • Open home page (we should be already logged in here)

Code that reproduces the issue.

  1. Cookies are retrieved with the page.cookies method, and then saved to a local file with fr.writeFile (works fine)
    let cookies = await page.cookies();

  2. Retrieve cookies from file (using fs.readFile, works fine too), and inject them using this method:
    ` /**
    * Inject cookies from previously saved cookies file
    * @param {string} file
    */
    async injectCookiesFromFile(file) {

     let cb = async function (_cookies) {
    
         console.log("Injecting cookies from file: %s", JSON.stringify(_cookies) );
         //await page.setCookie(..._cookies); // method 1
         await page.setCookie(_cookies); // method 2
     };
    
     fs.readFile(file, async function(err, data) {
    
         if(err)
             throw err;
         
         let cookies = JSON.parse(data);
         //await cb(cookies); // method 1
    
         for (var i = 0, len = cookies.length; i < len; i++)
             await cb(cookies[i]); // method 2
     });
    

    }`

  3. Open page
    await page.goto("https://www.linkedin.com/");

Note:
Using either method (1) or (2) (i.e. using a loop or using the spread operator) does not change anything.

Expected result?
Once the cookies have been set/injected with previously saved cookies, when we open the page, we should already be logged-in

What happens instead?
It seems the page.setCookie() had no effect, as when opening the page results in being still logged-out.

Any idea why the cookies seem to have no effect once injected?

@aslushnikov
Copy link
Contributor

@JoelEinbinder could you please take a look?

@JoelEinbinder
Copy link
Contributor

It works fine for me. Are you trying to immediately set the cookies after you click the button? You have to wait for the login to successfully complete first.

page.click('#login-submit'); // no await here
await page.waitForNavigation();
console.log(await page.cookies());

@xprudhomme
Copy link
Contributor Author

For some reasons, I had issues with the page.waitForNavigation() call. It kept on waiting until it timed out (even with the option waitUntil=networkidle). So currently, my only option is to wait for 10 sec, and it works !

So there is no issues with cookies, sorry about that, it's a page loading matter.

@nanhaiyufu
Copy link

@xavatar , do you mind share the code to write cookies to file, i have problem on that. ThankU!

@xprudhomme
Copy link
Contributor Author

xprudhomme commented Sep 18, 2017

Hey @nanhaiyufu ,

My code is wrapped into an object context, hence the this._page and other this.xxx properties references. But the main essence is:

 ```
  /**
   * Write Cookies object to target JSON file
   * @param {String} targetFile 
   */
  async saveCookies(targetFile) {

    let cookies = await this._page.cookies();
    return this.saveToJSONFile(cookies, this._cookiessPath + targetFile);
  }

  /**
   * Write JSON object to specified target file
   * @param {String} jsonObj 
   * @param {String} targetFile 
   */
  async saveToJSONFile(jsonObj, targetFile) {

    if( !/^\//.test(targetFile) )
      targetFile = this._jsonsPath + targetFile;
    
    return new Promise((resolve, reject) => {

      try {
        var data = JSON.stringify(jsonObj);
        console.log("Saving object '%s' to JSON file: %s", data, targetFile);
      }
      catch (err) {
        console.log("Could not convert object to JSON string ! " + err);
        reject(err);
      }
        
      // Try saving the file.        
      fs.writeFile(targetFile, data, (err, text) => {
        if(err)
          reject(err);
        else {
          resolve(targetFile);
        }
      });
      
    });
  }

Since everything is working fine now, I'm closing this issue.

@aslushnikov
Copy link
Contributor

@xavatar would you mind sharing a script (either here or in a new issue) where the waitForNavigation fails for you? This might be a bug.

@xprudhomme
Copy link
Contributor Author

xprudhomme commented Sep 18, 2017

I don't have the initial script anymore, but basically the steps are easy to reproduce:

  • goto() linkedin dot com
  • fill in the username + password fields with type()
  • click() on the login button
  • waitForNavigation()

I simply replaced the waitForNavigation step with a waitFor(10000), as it seemed to load forever...
Well, actually, the home page was loaded, but there were scripts still running in the background, even though the login process went ok.

Hope this could help.

@aslushnikov
Copy link
Contributor

@xavatar the following works for me on current tip-of-tree (0.11.0-alpha):

const puppeteer = require('puppeteer');
(async() => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://linkedin.com');
  await page.focus('input.login-email');
  await page.type('login'); // your login here
  await page.focus('input.login-password');
  await page.type('password'); // your password here
  page.click('.submit-button');
  await page.waitForNavigation();
  console.log('done!');
  await page.close();
  await browser.close();
})();

@xprudhomme
Copy link
Contributor Author

xprudhomme commented Sep 18, 2017

Yes, but the issue was getting the good cookies right away after the waitForNavigation() step, which did not seem to work properly, hence the need to add a waitFor(time) in my case.

However I haven't been able to reproduce this error every time, it's random... weird : /

@Kif11
Copy link

Kif11 commented Jan 21, 2018

@xprudhomme Thank you for sharing your code. It was very helpful!

@microwin168
Copy link

Wonderful ! Thanks for sharing.

@aslushnikov
Copy link
Contributor

This seems to be related to #921, which was recently fixed and should be already working fine in 1.10.0.

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

6 participants