LoginSignup
72
74

More than 1 year has passed since last update.

CentOS7上でSeleniumからGoogle Chromeのヘッドレスモードを利用する

Last updated at Posted at 2017-05-29

Google Chrome 59より実装されたヘッドレスモード(--headless)を利用したSeleniumの環境をCentOS7上に構築する。

環境

  • CentOS 7.3
    • yum install epel-releaseをしておく。
  • Google Chrome 60以上
  • ChromeDriver 2.29

ChromeDriverのインストール

EPELに登録されているバージョンが2.27なので、Downloads - Chrome Driver - WebDriver for Chromeよりダウンロードして展開する。
展開先はPATHが通っているとやりやすい。

wget https://chromedriver.storage.googleapis.com/2.29/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/local/bin/
sudo chown root:root /usr/local/bin/chromedriver

動作に必要なライブラリを入れる。

sudo yum install -y libX11 GConf2 fontconfig

Google Chromeのインストール

/etc/yum.repos.d/google-chrome.repoを作る。

[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

59はウィンドウサイズの指定が効かなかったので60(2017-05-30時点でunstable)のほうをインストールする。

sudo yum install -y google-chrome-unstable libOSMesa google-noto-cjk-fonts

libOSMesaはCentOS7におけるクラッシュへの対応のため。
https://bugs.chromium.org/p/chromium/issues/detail?id=695212

接続確認

Python版のSeleniumで動作を確認。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service


options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--window-size=1280,1024')
service = Service(executable_path='/path/to/chromedriver')
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://www.google.co.jp/')
print(driver.title) #=> Google
driver.save_screenshot('test.png')
driver.quit()

test.pngが文字化けしていなかったら恐らく正常。

72
74
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
72
74