🐙

GoogleColaboratoryでSeleniumを動かそうとすると出るエラーの話

2023/05/14に公開

✅目的

google colaboratoryでSeleniumを動かすことが思った以上に設定が必要だったためメモしします。

エラー内容

google colaboratoryの現在のubuntuバージョンではchromium-browserが配布されなくなったためインストールができません。

実行内容

!pip install chromium chromium-driver
!pip install selenium

実行内容(エラー)

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
ERROR: Could not find a version that satisfies the requirement chromium (from versions: none)
ERROR: No matching distribution found for chromium

解決方法

エラーが出ないSeleniumのインストール方法

%%shell

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


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


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

apt-get update
apt-get install chromium chromium-driver

pip install -q selenium

Seleniumの設定

from selenium import webdriver
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
download_path = '/content/csv'
options.add_experimental_option("prefs", {"download.default_directory": download_path})

動作確認

ドライバ生成

driver = webdriver.Chrome('chromedriver', options=options)
driver.implicitly_wait(10)

スクレイピング対象のページにアクセス

url="https://www.google.com/"
driver.get(url)

参考ページ

https://github.com/kaliiiiiiiiii/Selenium-Profiles/issues/10

Discussion