Puppeteerでオレオレ証明書(自己署名証明書)のWebページにアクセスする


はじめに

タイトルのとおりです。
Puppeteerのバージョンはv1.2で確認しています。

テストする

設定前

まず通常のPuppeteer起動の処理を書いてみます。

script.js
const url = 'https://[オレオレ証明書のページ]'

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

  const page = await browser.newPage();
  await page.goto(url);

     ・
     ・
    処理
     ・
     ・
})();

これを起動すると

(node:39231) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: net::ERR_CERT_AUTHORITY_INVALID at https://[オレオレ証明書のページ]
(node:39231) [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.

とエラーが出ます

設定後

追加するのは一行だけです。

script.js
const url = 'https://[オレオレ証明書のページ]'

async () => {
  const browser = await puppeteer.launch({
    ignoreHTTPSErrors: true, // ←ここだけ追加しました。
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
    ]
  });

  const page = await browser.newPage();
  await page.goto(url);

     ・
     ・
    処理
     ・
     ・
})();

と、以下の設定を入れてやるだけです。

ignoreHTTPSErrors: true,

これでオレオレ証明書ページもテストなりスクレイピングなりできますね。

それでは、よいPuppeteerライフをお過ごしください。