尝试过网上说的以下配置,均无效:
// args: ['--no-sandbox'],
// args: ['--no-sandbox', '--disable-setuid-sandbox'],
// ignoreDefaultArgs: ['--disable-extensions'],
报错信息如下:
f:\demo\autoDevtool\2019-9-17>node demo1.js
(node:18336) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome! spawn "C:Program Files (x86)GoogleChromeApplicationchrome.exe" ENOENT
TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
at onClose (f:\demo\autoDevtool\node_modules\puppeteer\lib\Launcher.js:348:14)
at ChildProcess.helper.addEventListener.error (f:\demo\autoDevtool\node_modules\puppeteer\lib\Launcher.js:339:64)
at ChildProcess.emit (events.js:182:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:238:12)
at onErrorNT (internal/child_process.js:407:16)
at process._tickCallback (internal/process/next_tick.js:63:19)
(node:18336) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18336) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled
will terminate the Node.js process with a non-zero exit code.
代码如下:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
// args: ['--no-sandbox'],
// args: ['--no-sandbox', '--disable-setuid-sandbox'],
// ignoreDefaultArgs: ['--disable-extensions'],
//粗心大意!!!下面的写法是错误的
executablePath: '"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"',
//正确写法如下:
executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
headless: false
})
const page = await browser.newPage()
await page.goto('http://www.baidu.com')
await page.screenshot({
//将会在当前目录下保存一张baidu.png
path: './baidu.png'
})
})()
路径改成\试试
单个反斜杠会被解释成转移符