目次
環境
Windows 7 64bit
node.js: 8.11.3
puppeteer: 1.9.0
はじめに
最近、puppeteerの本を買いました。
例アレです。
で、サンプルのシステムを動かしたところエラーが発生してしまいました。
エラーが発生
下記ソースを走らせたところエラーが発生しました。
javascript
const puppeteer = require('puppeteer');
(async () => {
const TARGET_URL = 'https://yahoo.co.jp';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(TARGET_URL);
await page.screenshot({ path: 'example.png' });
await browser.close();
})();
エラー
C:\puppeteer>node screenshot.js (node:366792) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 30000ms exceeded at Promise.then (C:\puppeteer\node_modules\puppeteer\lib\FrameManager.js:1230:21) at <anonymous> -- ASYNC -- at Frame.<anonymous> (C:\puppeteer\node_modules\puppeteer\lib\helper.js:144:27) at Page.goto (C:\puppeteer\node_modules\puppeteer\lib\Page.js:579:49) at Page.<anonymous> (C:\puppeteer\node_modules\puppeteer\lib\helper.js:145:23) at C:\puppeteer\screenshot.js:10:14 at <anonymous> (node:366792) 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:366792) [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 TARGET_URL = 'https://yahoo.co.jp';
const browser = await puppeteer.launch({
headless: false, // ヘッドレスブラウザをオフにする
slowMo: 50,
});
const page = await browser.newPage();
await page.goto(TARGET_URL);
await page.screenshot({ path: 'example.png' });
await browser.close();
})();
これで動作するようになりました。
まとめ

なんとか動くようになりました。
しかし原因は分からず・・・
なにか分かったら追記します。
今日はこの辺でー