我想在使用 puppeteer 编写的测试期间读取请求 cookie。但我注意到我检查的大多数请求只有引用和用户代理标头。如果我在 Chrome 开发工具中查看相同的请求,它们会有更多的标头,包括 Cookie。要查看它,请将下面的代码复制粘贴到https://try-puppeteer.appspot.com/ 中。
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.on('request', function(request) {
console.log(JSON.stringify(request.headers, null, 2));
});
await page.goto('https://google.com/', {waitUntil: 'networkidle'});
await browser.close();
Run Code Online (Sandbox Code Playgroud)
您可以访问和不能访问哪些请求标头是否有限制?是Chrome本身的限制还是puppeteer的限制?
感谢您的建议!
我有这个基本的帖子请求:
(async () => {
// Create browser instance, and give it a first tab
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
// Allows you to intercept a request; must appear before
// your first page.goto()
await page.setRequestInterception(true);
// Request intercept handler... will be triggered with
// each page.goto() statement
page.on('request', interceptedRequest => {
// Here, is where you change the request method and
// add your post data
var data = {
'method': 'POST',
'postData': …Run Code Online (Sandbox Code Playgroud)