如何重新创建包含所有Cookie的页面?

时间:2017-10-08 12:56:15

标签: javascript puppeteer

我想:

  1. 访问初始化会话的页面
  2. 将会话存储在JSON对象中
  3. 访问同一页面,该页面现在应该识别现有会话
  4. 我尝试的实现如下:

    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用于捕获已加载页面的Cookie。
    2. useSession应使用现有Cookie加载页面。
    3. 但是,这不起作用 - session.url页面无法识别会话。似乎并非所有的cookie都是以这种方式捕获的。

2 个答案:

答案 0 :(得分:7)

page#cookies似乎会返回一些session=true,expires=0配置的Cookie。 setCookie忽略了这些值。

我通过构建覆盖expiressession属性的新Cookie数组来解决这个问题。

const cookies = await page.cookies();

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

在撰写此答案时,未记录session属性。请参阅以下问题https://github.com/GoogleChrome/puppeteer/issues/980

答案 1 :(得分:0)

Puppeteer Traceback (most recent call last): File "c:\users\kevin\anaconda31\Lib\runpy.py", line 183, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "c:\users\kevin\anaconda31\Lib\runpy.py", line 142, in _get_module_details return _get_module_details(pkg_main_name, error) File "c:\users\kevin\anaconda31\Lib\runpy.py", line 109, in _get_module_details __import__(pkg_name) File "C:\Users\Kevin\.virtualenvs\Kevin-WjGwMqCZ\lib\site-packages\ipykernel\__init__.py", line 2, in <module> from .connect import * File "C:\Users\Kevin\.virtualenvs\Kevin-WjGwMqCZ\lib\site-packages\ipykernel\connect.py", line 18, in <module> import jupyter_client File "C:\Users\Kevin\.virtualenvs\Kevin-WjGwMqCZ\lib\site-packages\jupyter_client\__init__.py", line 4, in <module> from .connect import * File "C:\Users\Kevin\.virtualenvs\Kevin-WjGwMqCZ\lib\site-packages\jupyter_client\connect.py", line 23, in <module> import zmq File "C:\Users\Kevin\.virtualenvs\Kevin-WjGwMqCZ\lib\site-packages\zmq\__init__.py", line 49, in <module> from zmq import sugar File "C:\Users\Kevin\.virtualenvs\Kevin-WjGwMqCZ\lib\site-packages\zmq\sugar\__init__.py", line 7, in <module> from zmq.sugar import ( File "C:\Users\Kevin\.virtualenvs\Kevin-WjGwMqCZ\lib\site-packages\zmq\sugar\constants.py", line 8, in <module> from zmq.utils.constant_names import ( ModuleNotFoundError: No module named 'zmq.utils' 方法仅获取当前页面域的cookie。但是,在某些情况下,它也可能具有来自不同域的cookie。

您可以调用内部方法page.cookies()来从所有域中获取Cookie。

Network.getAllCookies

此处有关此主题的更多信息-Puppeteer get 3rd party cookies