LoginSignup
5
0

ChromeDriverのv.115以上で動かない時の対応

Posted at

対象者

  • 最新のChromeDriverのバージョンが取得できずシステムが動かない人
  • スクレイピングするひと
  • 自動でchromedriverのバージョンを更新したい・している人

発生したエラー

スクレイピングでChromeDriverを活用していますが、2023年8月に入って下記のようなエラーを連発していました。

2023/08/18 08:47:53 request unsuccessful: invalid session id

上記は、ChromeとChromeDriverのバージョン不一致によって起こるエラーです。
どうやらChromeDriverの最新のstableが取得できていないようでした。

結論

環境

  • Ubuntu
  • Docker

ChromeDriverに関するソース

公式サイトによると、v.115以降では対応が異なるらしい。

Current Releases
If you are using Chrome version 115 or newer, please consult the Chrome for Testing availability dashboard. This page provides convenient JSON endpoints for specific ChromeDriver version downloading.
For older versions of Chrome, please see below for the version of ChromeDriver that supports it.
For more information on selecting the right version of ChromeDriver, please see the Version Selection page.

実際にVersion Selectionのリンクにアクセスしてみると、Chromeのリリースプロセスと統合したのでChrome for Testingから取得してくれとのことです。

For versions 115 and newer
Starting with M115 the ChromeDriver release process is integrated with that of Chrome. The latest Chrome + ChromeDriver releases per release channel (Stable, Beta, Dev, Canary) are available at the Chrome for Testing (CfT) availability dashboard. As a result, you might no longer have a need for version selection — you could choose any available CfT version and simply download the correspondingly-versioned ChromeDriver binary.
For automated version downloading one can use the convenient CfT JSON endpoints.
If you still have a need for version selection (e.g. to match a non-CfT Chrome binary with a compatible ChromeDriver binary), look up the Chrome binary’s MAJOR.MINOR.BUILD version in the latest-patch-versions-per-build JSON endpoints to find the corresponding ChromeDriver version.
Alternatively, you can use the LATEST_RELEASE_ endpoints at the new location.

対応

  • やめる
    • http://chromedriver.storage.googleapis.com/116.0.5845.96/chromedriver_linux64.zip
  • 切り替える
    • https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/116.0.5845.96/linux64/chromedriver-linux64.zip

変更後のDockerfile

Dockerでない人も、ざっくり同じ対応です。

Dockerfile
FROM ubuntu:20.04

## 省略

RUN mkdir -p /home/root/src \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list \
    && apt-get update -qq \
    && apt-get install -y google-chrome-stable

# FIX: v.115以上のバージョンが含まれないため削除
# RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` \
# && curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip \
# && unzip /tmp/chromedriver_linux64.zip \
# && mv chromedriver /usr/local/bin/

RUN rm -rf /tmp/* /usr/local/bin/chromedriver

# stableバージョンが追いついていないためハードコード
RUN curl -sS -o /tmp/chromedriver-linux64.zip https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/116.0.5845.96/linux64/chromedriver-linux64.zip \
    && unzip /tmp/chromedriver-linux64.zip \
    && mv /chromedriver-linux64/chromedriver /usr/local/bin/

## 省略

またChromeDriverのバージョンをハードコードしている部分を、下記のように切り替えても良いかなと思います。1

Dockerfile
RUN CHROME_VERSION=`google-chrome-stable --version | sed 's/Google Chrome //g' | sed 's/ //g'` \
curl -sS -o /tmp/chromedriver-linux64.zip https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/$CHROME_VERSION/linux64/chromedriver-linux64.zip \
    && unzip /tmp/chromedriver-linux64.zip \
    && mv /chromedriver-linux64/chromedriver /usr/local/bin/

参考

  1. Chromeの最新バージョンを取得することで、マイナーバージョンがChromeDriverと異なります。また、TestingToolに該当のマイナーバージョンが存在しないことがあるのでご注意ください。

5
0
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
5
0