我想:
我尝试的实现如下:
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();
createSession
用于捕获已加载页面的Cookie。useSession
应使用现有Cookie加载页面。但是,这不起作用 - session.url
页面无法识别会话。似乎并非所有的cookie都是以这种方式捕获的。
答案 0 :(得分:7)
page#cookies
似乎会返回一些session=true,expires=0
配置的Cookie。 setCookie
忽略了这些值。
我通过构建覆盖expires
和session
属性的新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