Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 木偶演员:会议结束。很可能该页面已关闭_Node.js_Puppeteer - Fatal编程技术网

Node.js 木偶演员:会议结束。很可能该页面已关闭

Node.js 木偶演员:会议结束。很可能该页面已关闭,node.js,puppeteer,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/l

偶尔会有一个页面会很挑剔,出现以下错误:

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);
    });
}


希望它能帮助你解决问题。你可能不是在等待什么。否则页面就会崩溃。