LoginSignup
1
1

More than 1 year has passed since last update.

Dockerコンテナ上でSeleniumを使いたいけど初っ端のwebdriverが動かない

Posted at

はじめに

 Seleniumを使って個人開発しようと思い、コンテナ立ち上げて「さぁやるぞ!」という時にエラーに遭遇したので書き留めておきます。
 webdriverでchromeを使いたいけど、ChromeDriverの更新に合わせていちいちバージョンを上げていくのは面倒なので、ChromeDriverManagerを使い、常に最新のものにしておく方針でやろうとしました。Dockerfileにて、FROM python:3として公式pythonイメージをベースにコンテナを立ち上げてます。

コードとエラー

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager


browser = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
browser.get("https://www.google.com/")

上記のように実行してみると、以下のようなエラーコードが。

[WDM] - Downloading: 100%|█████████████████████████████████████████████████████████████████████████| 6.75M/6.75M [00:00<00:00, 10.6MB/s]
Traceback (most recent call last):
  File "/workspace/dl-ul-csv.py", line 12, in <module>
    browser = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__
    super().__init__(
  File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 101, in __init__
    self.service.start()
  File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/common/service.py", line 106, in start
    self.assert_process_still_running()
  File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/common/service.py", line 119, in assert_process_still_running
    raise WebDriverException(f"Service {self.path} unexpectedly exited. Status code was: {return_code}")
selenium.common.exceptions.WebDriverException: Message: Service /home/user/.wdm/drivers/chromedriver/linux64/112.0.5615/chromedriver unexpectedly exited. Status code was: 255

解決策

 どういうこっちゃ…?と検索したら、次のようなIssueコメントを見かけました。

Ubuntu 20.04 LTSでseleniumをchromium-browserで使いたいとのことですね。YMMV ですが、Ubuntu 20.04+ では chromium-browser が snap パッケージ以外では配布されなくなったので、Debian buster リポジトリから互換バージョンをインストールすることができます。

 このIssueはGoogle Colob上での話ですが、これっぽいような…。chromium-browserがパッケージ管理システムのsnap以外で配布されなくなったようです。自分のコンテナ上のOSはDebianですが、続くコメントにあるやり方で互換性のあるバージョンをインストールしてみました。

# Ubuntu no longer distributes chromium-browser outside of snap
#
# Proposed solution: https://askubuntu.com/questions/1204571/how-to-install-chromium-without-snap

# Add debian buster
cat > /etc/apt/sources.list.d/debian.list <<'EOF'
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates main
EOF

# Add keys
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A

apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-buster.gpg
apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-buster-updates.gpg
apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-buster.gpg

# Prefer debian repo for chromium* packages only
# Note the double-blank lines between entries
cat > /etc/apt/preferences.d/chromium.pref << 'EOF'
Package: *
Pin: release a=eoan
Pin-Priority: 500


Package: *
Pin: origin "deb.debian.org"
Pin-Priority: 300


Package: chromium*
Pin: origin "deb.debian.org"
Pin-Priority: 700
EOF

# Install chromium and chromium-driver
apt-get update
apt-get install chromium chromium-driver

# Install selenium
pip install selenium

 長いので.shファイルを作り、実行してみると無事にエラーが解消!これでコンテナ上でSeleniumが使えるようになりました。

余談

 今回は上記の方法で解消されましたが、Docker公式pythonイメージはOSがDebianだったので、Debian上でやるもっと簡単な方法もあるかもしれません…。

参考記事

Issues when trying to use Chromedriver in Colab · Issue #3347 · googlecolab/colabtools

1
1
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
1
1