node.js - Puppeteer:会话关闭。该页面很可能已关闭
问题描述
时不时地,一个页面会很挑剔,并且会出现以下错误:
UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.
at CDPSession.send (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:195:35)
at ExecutionContext._evaluateInternal (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:200:50)
at ExecutionContext.evaluate (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:106:27)
at DOMWorld.evaluate (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:79:24)
at emitUnhandledRejectionWarning (internal/process/promises.js:149:15)
at processPromiseRejections (internal/process/promises.js:211:11)
at processTicksAndRejections (internal/process/task_queues.js:98:32)
(node:38857) Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.
at CDPSession.send (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:195:35)
at ExecutionContext._evaluateInternal (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:200:50)
at ExecutionContext.evaluate (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:106:27)
at DOMWorld.evaluate (/Users/lancepollard/start/lancejpollard/data/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:79:24)
(node:38857) [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.
为什么会发生这种情况以及如何解决?
解决方案
我不知道您为什么会收到此错误,您可能应该将代码添加到问题中。
对我来说,这就是我处理粉碎浏览器/页面错误的方式:
async initiate() {
this.pageOptions = {
waitUntil: 'networkidle2',
timeout: 60000
};
puppeteerExtra.use(pluginStealth());
this.browser = await puppeteerExtra.launch({ headless: false });
const browserWSEndpoint = await this.browser.wsEndpoint();
puppeteerExtra.connect({ browserWSEndpoint: browserWSEndpoint });
this.page = await this.browser.newPage();
await this.page.setRequestInterception(true);
this.page.on('request', (request) => {
if (['image', 'stylesheet', 'font', 'script'].indexOf(request.resourceType()) !== -1) {
request.abort();
} else {
request.continue();
}
});
this.page.on('dialog', async dialog => {
await dialog.dismiss();
});
}
wait = (ms) => new Promise(resolve => setTimeout(resolve, ms))
async restart() {
await this.close();
await this.wait(1000);
this.initiate();
}
async close() {
if (this.browser) {
await this.page.close();
await this.browser.close();
this.browser = null;
this.page = null;
this.pageOptions = null;
}
}
以及爬取过程:
crawl(link, userAgent) {
return new Promise(async (resolve, reject) => {
if (reject) { }
// Limit the runtime of this function in case of stuck URL crawling process.
setTimeout(async () => {
await this.restart();
resolve(null);
return;
}, 60000);
if (!userAgent) {
userAgent = crawlUtils.getRandomUserAgent();
}
const crawlResults = { isValidPage: true, pageSource: null };
if (!this.page) {
await this.wait(1000);
resolve(null);
return;
}
try {
await this.page.setUserAgent(userAgent);
await this.page.goto(link, this.pageOptions);
await this.page.waitForFunction(this.waitForFunction);
crawlResults.pageSource = await this.page.content();
}
catch (error) {
crawlResults.isValidPage = false;
}
if (this.isLinkCrawlTest) {
await this.close();
}
resolve(crawlResults);
});
}
希望它可以帮助您解决您的问题。
推荐阅读
- swift - swift - 如何检测从哪个 ViewController 应用程序进入后台?
- python - python salesforce 没有连接,回家还好,不工作。防火墙/安全?
- javascript - 检查子 div 是否为空以从父元素中删除属性
- amazon-web-services - 使用 golang 的 AWS S3 并行下载
- c# - 如何从用户端保护 Web api
- docker - 使用 https 访问 Docker 私有注册表
- ios - swift 4.2 无法将类型“(_)-> Void”的值转换为预期的参数类型“(()-> Void)?”
- reactjs - 使用 Github Pages 部署的 React 项目出现 404 问题
- java - 为什么在 java 6 和 java 7 中获取字符 ®(U+00AE) 不同?
- qt - 与海洋内的 Public\Qt 动态链接