Puppeteer错误:An `executablePath` or `channel` must be specified for `puppeteer-core`
回答 2
浏览 1万
2022-10-30
我正在尝试做一个简单的Puppeteer项目。我目前的代码只是一个测试。但它并不工作。
import bypass from './captcha/captchaBypasser.js';
import {createRequire} from "module";
const require = createRequire(import.meta.url);
const puppeteer = require('puppeteer-extra');
const hidden = require('puppeteer-extra-plugin-stealth')
test()
async function test() {
// Launch sequence
puppeteer.use(hidden())
const browser = await puppeteer.launch({
args: ['--no-sandbox',],
headless: false,
ignoreHTTPSErrors: true
})
const page = await browser.newPage()
await page.setViewport({
width: 1920,
height: 1280,
deviceScaleFactor: 1,
});
//Go to page
await page.goto('https://google.com/', {
waitUntil: 'networkidle0',
});
}
我到处寻找解释,但似乎只有我一个人得到了这个错误
throw new Error(message);
^
Error: An `executablePath` or `channel` must be specified for `puppeteer-core`
at assert (C:\Users\Julian\Desktop\project\node_modules\puppeteer-core\lib\cjs\puppeteer\util\assert.js:28:15)
at ChromeLauncher.launch (C:\Users\Julian\Desktop\project\node_modules\puppeteer-core\lib\cjs\puppeteer\node\ChromeLauncher.js:69:36)
at PuppeteerNode.launch (C:\Users\Julian\Desktop\project\node_modules\puppeteer-core\lib\cjs\puppeteer\node\PuppeteerNode.js:154:105)
at PuppeteerExtra.launch (C:\Users\Julian\Desktop\project\node_modules\puppeteer-extra\dist\index.cjs.js:128:41)
at async test (file:///C:/Users/Julian/Desktop/project/test.js:21:19)
Node.js v18.12.0
我本以为会弹出一个Puppeteer实例并转到google.com,结果却出现了一个错误。
请分享你的package.json和操作系统。
- ggorlen 2022-10-30
2 个回答
#1楼
已采纳
得票数 31
现在看来,puppeteer-core现在需要executablePath
是强制性的。
试试这个吧。
import bypass from './captcha/captchaBypasser.js';
import {createRequire} from "module";
const require = createRequire(import.meta.url);
const puppeteer = require('puppeteer-extra');
const hidden = require('puppeteer-extra-plugin-stealth')
// require executablePath from puppeteer
const {executablePath} = require('puppeteer')
test()
async function test() {
// Launch sequence
puppeteer.use(hidden())
const browser = await puppeteer.launch({
args: ['--no-sandbox',],
headless: false,
ignoreHTTPSErrors: true,
// add this
executablePath: executablePath(),
})
const page = await browser.newPage()
await page.setViewport({
width: 1920,
height: 1280,
deviceScaleFactor: 1,
});
// Go to page
await page.goto('https://google.com/', {
waitUntil: 'networkidle0',
});
}
使用puppeteer中的
executablePath
要求对我来说造成了一个typecript错误。TS2322: Type '(channel?: ChromeReleaseChannel) => string' is not assignable to type 'string'.
我现在直接使用PUPPETEER_EXECUTABLE_PATH
env中的executeablePath,工作正常。
- Fabian S. 2022-11-14
#2楼
得票数 2
我想这是一个新问题。我之前工作的代码在更新puppeteer版本后,现在收到了同样的错误。尝试重新安装旧版本的 puppeteer,或者可能需要旧版本的 node.js。