node.js - UnhandledPromiseRejectionWarning:错误:协议错误(Network.setCookies):目标已关闭 - 无法设置 cookie
问题描述
目前我遇到了一个问题,即 Puppeteer 在使用 setCookies 方法时崩溃。我目前正在使用 Puppeteer v 1.4.0(撰写本文时的最新版本)以及与 Puppeteer 捆绑的 Chromium 版本,这是给我带来麻烦的代码:
const puppeteer = require('puppeteer');
const moment = require('moment');
(async () => {
const browser = await puppeteer.launch(
{
headless: false
}
);
const page = await browser.newPage();
await page.goto('https://google.com');
const currentUrl = await page.url();
await browser.close();
const browser1 = await puppeteer.launch(
{
headless: false
}
);
const page1 = await browser1.newPage();
const cookie = await currentUrl.split("/");
await page1.setCookie({
'name': 'samplename',
'value': cookie[0],
'domain': 'sampledomain',
'path': cookie[0] + '/' + cookie[0] + '/' + cookie[0],
'expires': moment().add(21, 'days').valueOf(),
'httpOnly': false,
'secure': true,
'sameSite': "Lax"
});
await page1.goto(currentUrl);
})();
这是错误消息
(node:64704) UnhandledPromiseRejectionWarning: Error: Protocol error (Network.setCookies): Target closed.
at Promise (/Users/pc/Desktop/Shopify Bot/node_modules/puppeteer/lib/Connection.js:200:56)
at new Promise (<anonymous>)
at CDPSession.send (/Users/pc/Desktop/Shopify Bot/node_modules/puppeteer/lib/Connection.js:199:12)
at Page.setCookie (/Users/pc/Desktop/Shopify Bot/node_modules/puppeteer/lib/Page.js:320:26)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:182:7)
(node:64704) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:64704) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我已经自己研究了一段时间,并且多个消息来源似乎说实际上没有执行异步是问题,但是,我相信我正在异步运行所有内容(但是,因为这是我第一次做任何事情在 NodeJS 上异步,我可能会在判断中犯严重错误)。我已经尝试验证我的 Chromium 并卸载 + 重新安装 Puppeteer,但似乎没有任何效果。
解决方案
推荐阅读
- c - 使用 getchar 读取用户输入并创建二维矩阵
- css - 垂直和水平行居中引导面板
- java - 这会像析构函数一样工作吗?
- vba - 从另一个模块调用私有子
- ios - UICollectionView 如何找到单元格最终所在的行
- phpmyadmin - 我在哪里可以找到 myphpadmin 上的 config.inc.php
- r - dplyr 按组汇总,同时跨日期和增量分配小时数
- sharepoint-online - SP.Utilities.Utility.SendEmail 访问被拒绝
- sql - 使用 SQL Report Builder 如何过滤掉所有 Id,每个值 NE 另一个字段值?
- excel - 活动列的条件格式宏