Puppeteer错误 TypeError: browser.newPage is not a function处理

 

复制代码
// 打开一个浏览器
    async initialize(){
        this.browser = puppeteer.launch({headless: false})
    }
    // 打开新的页签
    async newPage(){
        let { device } = this.options
        console.log(this.browser)
        // 打开一个新的页签
        let page = await this.browser.newPage()
        // 选择一个适配设备
        await page.emulate(puppeteer.devices[device])
        return page
    }
    async genHtml(url){
        let page = await this.newPage()
        let res = page.goto(url, {waitUntil: 'networkidle2'})
        if(res && !res.ok()){
            // 访问失败 抛出异常
            throw new Error(`${res.status} on ${url}`)
        }
        return 'html'
    }
复制代码

报错   TypeError: browser.newPage is not a function

错误代码中调用launch立刻返回,browser并未指向任何实例。因此browser.newPage()方法爆出is not a function错误。在puppeteer.launch()前加上await问题解决。

 

posted @   leahtao  阅读(705)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示