LoginSignup
32
19

More than 5 years have passed since last update.

Amazon Linux 2 で Headless Chrome を動かしてみた

Last updated at Posted at 2017-12-16

概要

Amazon Linux で Google Chrome を動かそうとすると、パッケージの依存関係の問題でインストールがなかなかうまくいきません。

Amazon Linux 2 の LTS 版が出たので、早速再挑戦してみました。

試した環境はこんな感じ。

$ cat /etc/system-release

Amazon Linux 2

Google Chrome をインストール

Linux 版が RPM で配布されているのでこれを使用します。

$ sudo yum -y install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

パッケージの依存関係のエラーは出ず、インストールは完了。

Headless モードで動かしてみる

スクリーンショットを撮ってみます。

$ google-chrome --headless --disable-gpu --window-size=1024,768 --hide-scrollbars --screenshot='yahoo.png' https://www.yahoo.co.jp

[0716/141244.079664:ERROR:gpu_process_transport_factory.cc(1017)] Lost UI shared context.
Fontconfig warning: "/etc/fonts/fonts.conf", line 146: blank doesn't take any effect anymore. please remove it from your fonts.conf
[0716/141245.205569:INFO:headless_shell.cc(590)] Written to file yahoo.png.

エラーは出ましたがスクリーンショットを保存した画像ファイルが生成されました。

結果

yahoo.png

残念。
スクリーンショットは撮れましたが、日本語が出ません。

日本語フォントをインストールしてからもう一度。

$ sudo yum -y install ipa-gothic-fonts

$ google-chrome --headless --disable-gpu --window-size=1024,768 --hide-scrollbars --screenshot='yahoo.png' https://www.yahoo.co.jp

[0716/142052.156208:ERROR:gpu_process_transport_factory.cc(1017)] Lost UI shared context.
Fontconfig warning: "/etc/fonts/fonts.conf", line 146: blank doesn't take any effect anymore. please remove it from your fonts.conf
[0716/142053.271844:INFO:headless_shell.cc(590)] Written to file yahoo.png.

同じエラーが出ていますが、スクリーンショットを保存した画像ファイルが生成されました。

結果

yahoo.png

成功🎉

発生したエラーについて

Fontconfig warning: "/etc/fonts/fonts.conf", line 146: blank doesn't take any effect anymore. please remove it from your fonts.conf

このエラーは Fontconfig の設定ファイル( /etc/fonts/fonts.conf )の変更で対応。

blank タグの定義が不要になったので、<blank></blank> を消してしまうか、

<!-- <blank> -->
※この間はそのまま
<!-- </blank> -->

のようにコメントアウトで対応。

[????/??????.??????:ERROR:gpu_process_transport_factory.cc(1017)] Lost UI shared context.

このエラーは Chrome を Headless モードで実行した時に発生するらしい。対応方法については現時点では不明。Chrome のアップデートで解消されることを期待。

まとめ

Headless Chrome を使えば、Webサイトのサムネイルを作ったり、 Selenium でごにょごにょしたりといろいろできます。

Amazon Linux 2 のLTS版が出たので、これからいろいろなAWSサービスでも使えるようになるかもしれませんね。

Lambda や Fargate のようなサーバレスなサービスでも使えるようになると面白そう。

32
19
1

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
32
19