How to recreate a page with all of the cookies?

谁说我不能喝 提交于 2019-12-10 18:13:42

问题


I am trying to:

  1. Visit a page that initialises a session
  2. Store the session in a JSON object
  3. Visit the same page, which now should recognise the existing session

The implementation I have attempted is as follows:

import puppeteer from 'puppeteer';

const createSession = async (browser, startUrl) => {
  const page = await browser.newPage();

  await page.goto(startUrl);

  await page.waitForSelector('#submit');

  const cookies = await page.cookies();
  const url = await page.url();

  return {
    cookies,
    url
  };
};

const useSession = async (browser, session) => {
  const page = await browser.newPage();

  for (const cookie of session.cookies) {
    await page.setCookie(cookie);
  }

  await page.goto(session.url);
};

const run = async () => {
  const browser = await puppeteer.launch({
    headless: false
  });

  const session = await createSession(browser, 'http://foo.com/');

  // The session has been established
  await useSession(browser, session);
  await useSession(browser, session);
};

run();
  1. createSession is used to capture the cookies of the loaded page.
  2. useSession are expected to load the page using the existing cookies.

However, this does not work – the session.url page does not recognise the session. It appears that not all cookies are being captured this way.


回答1:


It appears that page#cookies returns some cookies with the session=true,expires=0 configuration. setCookie ignores these values.

I worked around this by constructing a new cookies array overriding the expires and session properties.

const cookies = await page.cookies();

const sessionFreeCookies = cookies.map((cookie) => {
  return {
    ...cookie,
    expires: Date.now() / 1000 + 10 * 60,
    session: false
  };
});

At the time writing this answer, session property is not documented. Refer to the following issue https://github.com/GoogleChrome/puppeteer/issues/980.




回答2:


Puppeteer page.cookies() method only fetches cookies for the current page domain. However, there might be cases where it can have cookies from different domains as well.

You can call the internal method Network.getAllCookies to fetch cookies from all the domains.

(async() => {
  const browser = await puppeteer.launch({});
  const page = await browser.newPage();
  await page.goto('https://stackoverflow.com', {waitUntil : 'networkidle2' });

  // Here we can get all of the cookies
  console.log(await page._client.send('Network.getAllCookies'));

})();

More on this thread here - Puppeteer get 3rd party cookies



来源:https://stackoverflow.com/questions/46631333/how-to-recreate-a-page-with-all-of-the-cookies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!