在本地找不到预期的浏览器 chrome

Fre*_* J. 43 node.js meteor puppeteer

此 Meteor 代码使用“puppeteer 8.0.0”、“puppeteer-core 10.0.0”、puppeteer-extra 3.1.18”和“puppeteer-extra-plugin-stealth 2.7.8”,它给出以下错误:

错误:无法在本地找到所需的浏览器(chrome)。运行npm install以下载正确的 Chromium 修订版 (884014)。

尝试“npm install”无济于事。在线阅读,尝试从 package.json 依赖项中删除 "puppeteer-core": "^10.0.0" 但无济于事。

非常感谢任何帮助。谢谢

const puppeteer = require('puppeteer-extra');
const nameH = require('./NameH');

const puppeteerOptions = {
    headless: true,
    ignoreHTTPSErrors: true,
    args: ['--no-sandbox', '--single-process', '--no-zygote', '--disable-setuid-sandbox']
}

let browser;
let pageNameH;

const init = async () => {
    const StealthPlugin = require('puppeteer-extra-plugin-stealth');
    console.log('1')         //>>>>>>>>>>>> Prints 1
    puppeteer.use(StealthPlugin());
    console.log('2')         //>>>>>>>>>>>> Prints 2
    
    browser = await puppeteer.launch(puppeteerOptions);
    console.log('3') //>>>>>>>>> DID NOT PRINT <<<<<<<<<<<<<<<
    pageNameH = await browser.newPage();
    console.log('4')

    await pageNameH.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
    await pageNameH.setViewport({ width: 1366, height: 768 });
    await pageNameH.setRequestInterception(true);
    blockResources(pageNameH);
}

const blockResources = page => {
    page.on('request', (req) => {
    if (req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image') {
        req.abort();
    }
    else {
        req.continue();
    }
    });
}


export const abc = async (nm, loc) => {
    try {
    console.log('name try')       //>>>>>>>>>>>> Prints "name try"
    if (!(browser && pageNameH))
        await init();
    //use "required" nameh here

    } catch (error) { // print the error <<<<<<<<<<<<<<<<<<<<<<<<<
    console.log("Could not launch Puppeteer or open a new page.\n" + error);
    if (browser && browser.close === 'function') await browser.close();
    }
}

Run Code Online (Sandbox Code Playgroud)

// included in package.json
"dependencies": {
    "@babel/runtime": "^7.11.2",
    "axios": "^0.21.1",
    "check": "^1.0.0",
    "cheerio": "^1.0.0-rc.6",
    "jquery": "^3.5.1",
    "meteor-node-stubs": "^1.0.1",
    "nightmare": "^3.0.2",
    "pending-xhr-puppeteer": "^2.3.3",
    "puppeteer": "^8.0.0",
    "puppeteer-core": "^10.0.0",
    "puppeteer-extra": "^3.1.18",
    "puppeteer-extra-plugin-adblocker": "^2.11.11",
    "puppeteer-extra-plugin-block-resources": "^2.2.9",
    "puppeteer-extra-plugin-stealth": "^2.7.8"
},
Run Code Online (Sandbox Code Playgroud)

小智 98

也许你可以尝试这个,它对我在 linux(centOS)、puppeteer(10.2.0) 上有效。

cd ./node_modules/puppeteer
npm run install
Run Code Online (Sandbox Code Playgroud)

如果失败,您还可以尝试运行:

cd ./node_modules/puppeteer
npm install
Run Code Online (Sandbox Code Playgroud)

这会将 chromium 下载到 ./node_modules/puppeteer/.local-chromium

  • puppeteer@19.2.2:现在是“npm run postinstall”。或“yarn run postinstall”您使用的每一个。 (4认同)

小智 21

通过运行此命令手动安装 chromium 来修复它。

node node_modules/puppeteer/install.js
Run Code Online (Sandbox Code Playgroud)

  • 我只需运行它,现在它是一个 .mjs 文件而不是 .js,但它的工作原理都是一样的! (2认同)

Alx*_*Alx 9

我有同样的问题。我检查了我的环境变量,即使 PUPPETEER_SKIP_CHROMIUM_DOWNLOAD 设置为 false,它仍然无法工作。在我删除变量(对于 mac 取消设置 PUPPETEER_SKIP_CHROMIUM_DOWNLOAD)后,它起作用了。

相关依赖:

  "dependencies": {
    "chrome-aws-lambda": "^10.0.0",
    "puppeteer-core": "^10.0.0",
  },
  "devDependencies": {
    "puppeteer": "^10.0.0",
  }
Run Code Online (Sandbox Code Playgroud)

启动铬:

    import chromium from "chrome-aws-lambda";

    const browser = await chromium.puppeteer.launch({
        executablePath: await chromium.executablePath,
    });
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您在本地测试,请确保您已将 puppeteer 安装为开发依赖项。具体来说

npm install puppeteer --save-dev
Run Code Online (Sandbox Code Playgroud)

https://github.com/alixaxel/chrome-aws-lambda/wiki/HOWTO:-Local-Development#workaround

这种方法使我们能够依靠 puppeteer 进行本地开发,并依靠 puppeteer-core 进行生产部署。