概要
- Ubuntuを24.04にアップグレードしたところ、今まで動いていたSeleniumを使ったPythonスクリプトが動かなくなったので、試行錯誤の結果、SeleniumのOptionの
--single-process
を除くと動いた、という話です
環境
$ cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.2 LTS (Noble Numbat)"
$ google-chrome --version
Google Chrome 135.0.7049.95
$ chromedriver --version
ChromeDriver 135.0.7049.95 (de2eb485a1951079e63bdb57ce25544d2dc79c15-refs/branch-heads/7049@{#1836})
$ python -V
Python 3.11.2
$ pip list
pip 25.0.1
selenium 4.31.0
動いたコード
from selenium import webdriver
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
#options.add_argument("--single-process") # これをつけると、Ubuntu24.02で動かない
options.add_argument("--disable-dev-shm-usage")
url = "http://abehiroshi.la.coocan.jp/"
driver = webdriver.Chrome(options=options)
driver.get(url)
print(driver.title)
driver.quit()
# 実行すると、"阿部寛のホームページ"が返ってくる
参考資料
- Seleniumユーザーを散々悩ませてきた、「ChromeブラウザのバージョンとChromeDriverのバージョンが合わない問題」について。Selenium4.10以上は、https://yuki.world/python-selenium-chromedriver-auto-update/で述べられているように、SeleniumにSelenium ManagerというChromeDriver自動更新機能が追加されたので、この問題に頭を悩ませなくて良くなった。