7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Puppeteer入門 Googleの検索結果を表示するサンプルアプリ

Posted at

はじめに

Puppeteerの使い方メモ。
アプリのセットアップから、Google検索できるまでの方法をメモ。

事前作業

Node.jsをインストールしておくこと。

手順

  • 任意のフォルダを作成し、移動。
  • 以下のコマンドを実行し、npmを初期化&Puppeteerインストール
npm init

~その後、アプリ名等を記載(ここでは省略)~

npm install puppeteer
  • test.jsをファイルを作成。
test.js
const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch({
  headless: false,
  defaultViewport: { width: 2048, height: 1048 }
});
const page = await browser.newPage();

// Goolgeのページに移動
await page.goto("https://www.google.co.jp/");

// 検索ワードを入力 以下の例では"Puppeteer"
await page.type('input[title="検索"]', "Puppeteer", { delay: 50 });

// 検索ボタンをクリック
await page.evaluate(() => {
  document.querySelector('input[value^="Google"]').click();
});

// ページ遷移を待ちます。
await page.waitForNavigation({timeout: 600000, waitUntil: "domcontentloaded"});

  console.log('検索結果がブラウザに表示されました。');
})();
  • 以下のコマンドで実行
$ node test.js
7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?