首页 > 解决方案 > Puppeteer 无法在 GoormIDE 中启动浏览器进程

问题描述

const puppeteer = require ('puppeteer');

async function scrapeA (url) {
    return new Promise (async (resolve, reject) => {
        try{
            **const browser = await puppeteer.launch();**

            const page = await browser.newPage();
            await page.goto(url)
            page.$eval('#city', elem => elem.click());

            const [el] = await page.$x('//*[@id="content"]/div[1]/div[2]/h1/a');
            const txt1 = await el.getProperty('innerText');
            const nameA =  await txt1.jsonValue();

            const [el2] = await page.$x('//*[@id="offerPrice_328504"]');
            const txt2 = await el2.getProperty('innerText');
            const priceA =  await txt2.jsonValue();

            const [el3] = await page.$x('//*[@id="inventoryTableBody"]/div[5]/div[3]/span[1]');
            const txt3 = await el3.getProperty('innerText');
            const quantA =  await txt3.jsonValue();

            const [el4] = await page.$x('//*//*[@id="inventoryTableBody"]/div[5]/div[2]/text()[1]');
            const txt4 = await el4.getProperty('textContent');
            const locA =  await txt4.jsonValue();

            console.log({nameA},{priceA},{quantA},{locA});

            browser.close();
            return resolve();
        } catch (e) {
            return reject(e);
        }    
    })
}
scrapeA(webpageurl);

这段代码在我的代码编辑器中工作。我正在尝试对此进行一些后端处理,所以我将它移到了我的 GoormIDE 中。浏览器进程无法启动

我已经尝试了 2 个不同软件包的所有组合:puppeteer 和 puppeteer-core

和 3 种不同的启动设置:

const browser = await puppeteer.launch();
const browser = await puppeteer.launch({headless: false}); 
const browser = await puppeteer.launch({ignoreDefaultArgs: ['--disable-extensions']});

标签: node.jsidepuppeteer

解决方案


使用安装缺少的包

apt-get install gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget libgbm-dev

试试这个

const browser = await puppeteer.launch({ args: ['--no-sandbox'] });

推荐阅读