LoginSignup
99
87

More than 3 years have passed since last update.

Amazon LinuxでSelenium環境を最短で構築する

Last updated at Posted at 2017-12-24

Amazon Linuxのサポートが2020年6月30日に終了とのことで、
Amazon Linux2でSelenium環境を最短で構築する を新たに公開しましたので、
Amazon Linux2でのSelenium環境を構築したい方はそちらをご参考頂ければと思います。


この記事は、ケーシーエスキャロット Advent Calendar 2017の25日目の記事です。

本日はついにクリスマスですね。皆様いかがお過ごしでしょうか。
私はクリスマスイブに一人でラーメンを食べに行きました。おいしかったです。
今日の予定は職場と自宅の往復です。よろしくお願いします。

概要

Seleniumを使って色々してみたくなったので、Amazon LinuxのEC2に
ChromeDriverとGoogle Chromeを入れようとしたが、依存関係やら何やらで一向に動く状態にならない。
ようやく最小の手順が分かったのでメモ。以下は全てrootで実施したものです。

手順

Google Chrome のインストール
GConf2 のインストール
ChromeDriver のインストール
Google Noto Fonts のインストール
Selenium のインストール

Google Chromeのインストール

yum install google-chrome-stableでやると依存関係で死にます。
以下を実行しましょう。

curl https://intoli.com/install-google-chrome.sh | bash

最新のGoogle Chromeがうまく入るはずです。

GConf2のインストール

ChromeDriverを実行する時にこれがないと動きません。
yum install GConf2をやっても No package GConf2 available. となるので、
リポジトリを追加します。
vim /etc/yum.repos.d/centos.repoで以下を記載。

centos.repo
[CentOS-base]
name=CentOS-6 - Base
mirrorlist=http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#released updates
[CentOS-updates]
name=CentOS-6 - Updates
mirrorlist=http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=updates
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[CentOS-extras]
name=CentOS-6 - Extras
mirrorlist=http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=extras
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

GPG keyをインポートします。

rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6 

これでGConf2がインストール出来るようになります。

yum -y install GConf2

ChromeDriverのインストール

現時点でインストールしたGoogle Chromeが63なので、それに対応する2.34を取得します。
Google Chromeのバージョンはgoogle-chrome-stable -versionで確認できます。
※ Google Chromeのバージョンと対応するChromeDriverのバージョンの確認はこちら

wget https://chromedriver.storage.googleapis.com/2.34/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/local/bin/

Google Noto Fontsのインストール

このままだとSeleniumで画面のスクリーンショットを撮った際に
日本語が文字化けしてしまうのでフォントをインストールします。
https://www.google.com/get/noto/

cd ~/Downloads   # どこか適当な場所で
wget https://noto-website-2.storage.googleapis.com/pkgs/Noto-hinted.zip
unzip Noto-hinted.zip
mkdir -p /usr/share/fonts/opentype/noto
cp *otf *ttf /usr/share/fonts/opentype/noto
fc-cache -f -v # optional

Seleniumのインストール

シンプルにいきましょう。

pip install selenium

インストールするものは以上になります。

Seleniumを使ってみよう

googleのトップページのスクリーンショットを撮る簡単なテストコードを作成して
実行してみましょう。

test.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--window-size=1280,1024')
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.google.co.jp/')
driver.save_screenshot('test.png')

実行ディレクトリにtest.pngが作成されます。

ちなみにoptionsに追加している引数ですが、
・--headlessでヘッドレスモード
 (これで実行しないと動きません)
・--no-sandboxはgoogle-chrome-stableが動くために必要
 (google-chrome-stableを実行すると分かるはず)
・--disable-gpuで描画周りが安定するらしい
 (これ無しで実行したら作られたpngファイルが真っ黒だった)
・--window-sizeはそのままウィンドウサイズの指定
となります。

最後に

作成されたtest.pngは以下のようになっていました。文字化けしていませんね。
test.png

僕のサンタさんはどこにいるのでしょうか?
Googleマップで探しても見つかりませんでした。
来年は来るといいなぁ...

99
87
2

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
99
87