无法在Google云功能中调试Puppeteer超时

问题描述

我已经进行了很多调试,并阅读了文章,但无法弄清楚为什么我得到了

at Promise.then (/workspace/node_modules/puppeteer/lib/cjs/puppeteer/common/LifecycleWatcher.js:106:111) name: 'TimeoutError' }

对于下面的“ goto”行,我尝试调整参数,并尝试从5到4到3改回package.json中的puppeteer版本。代码在本地运行良好,但在Google Cloud Function中运行保持超时。我通过为google.com编写一个简单的提取功能来验证我的VPC连接器是否正常工作,所以这纯粹是GCF问题中的伪人。

仅供参考,这是在PubSub主题上触发的。

const puppeteer = require('puppeteer')

const PUPPETEER_OPTIONS = {
  headless: true,args: [
    '--disable-gpu','--disable-dev-shm-usage','--disable-setuid-sandbox','--no-first-run','--no-sandbox','--no-zygote','--single-process',"--proxy-server='direct://'",'--proxy-bypass-list=*',],};

const closeConnection = async (page,browser) => {
  page && (await page.close());
  browser && (await browser.close());
};

exports.runScraper = async (message,context) => {
    const url = Buffer.from(message.data,'base64').toString()
    console.log( `triggered with ${url}`)
    
    const browser = await puppeteer.launch(PUPPETEER_OPTIONS);
    const page = await browser.newPage();

    try // open url and get price and title
    {
        console.log( "awaiting goto")
        await page.goto(url,{ waitUntil: 'networkidle2' })
        console.log( "awaiting evaluate")
        let item = await page.evaluate( async () => {
            let priceArray = document.querySelector('div.cAIbCF').innerText.split('.')
            return {
                title: document.querySelector('h1 > span').innerText,whole: priceArray[0],part: priceArray[1]
            }
        }) 
    } // try
    catch (error) {
        console.log( error );
        throw error;
    } finally {
        console.log( "finally closeConnection" );
        await closeConnection(page,browser);
        return;
    }
}

解决方法

我遇到了类似的问题。 我改变了

await page.goto(url,{ waitUntil: 'networkidle2' })

await page.goto(url,{
    waitUntil: 'load',timeout: 0
});

成功了。请随时使用它,并告诉它是否有效。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...