0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ChromeDriverをダウンロードするShellScriptを作る(With Git Bash)

Posted at

目的

自動化のため、ChromeDriverを使用しています。ほんとにちょっとした自動化なので、サーバなどでの定期実行ではなく、メインマシンのWindowsでコマンドをたたいて実行しています。メインマシンなのでChromeは常に最新版にしています。そのため、気づくとChromeDriverのバージョンが合わなくなり(古くなり)、自動化が失敗するようになります。いちいち手作業でChromeDriverを更新するのもだるくなったので、更新の自動化を考えました。

実行環境

  • Windows 11
  • git version 2.49.0.windows.1
  • Git for Windows v2.49.0.windows.1

ChromeDriverは、決まったディレクトリに設置しています。更新するときは、Win64bit用の最新版バイナリのアーカイブをダウンロードしてきて展開し、古いchromedriver.exeを新しいファイルで上書きします。

@puppeteer/browsers を使った方法

ChromeDriver更新自動化ができそうなものには、Chrome for Testing: ブラウザの自動化のための信頼性の高いダウンロード  |  Blog  |  Chrome for Developersがあります。また、npmから入手できるコマンドラインユーティリティーがあるようです。試してみたところ、WindowsのGit Bash環境でも、以下のようにダウンロードできました。

cd ~/test/

npx @puppeteer/browsers install chromedriver@latest >downloaded.txt
# 以下のパスにダウンロードされた
# .\chromedriver\win64-139.0.7210.0\chromedriver-win64\chromedriver.exe

awk '{print $1}' downloaded.txt # ダウンロードしたバージョンを出力
# 出力例:
# chromedriver@139.0.7210.0

awk '{print $2}' downloaded.txt # chromedriver.exeのパスを表示
# 出力例:
# C:\Users\user\test\chromedriver\win64-139.0.7210.0\chromedriver-win64\chromedriver.exe

chromedriver配下がCacheディレクトリとなるようで、必ずこのディレクトリに「プラットフォーム」+「バージョン情報」の名前でディレクトリが作られ、ダウンロードされます。

自力でダウンロードする方法

今回は指定の場所にchromedriver.exeを直接ダウンロード・設置したかったので、以下の手順を使いました。JSON APIエンドポイントが提供されているため、このJsonをjqで解析してダウンロード後unzipコマンドで展開します。ChromeDriverの最新版は以下のURLから取得できます。

curl https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json

以下のようなJsonが取得できます。platformを特定すれば、ChromeDriverのバイナリファイルのURLが分かります。

{
  "timestamp": "2025-05-30T23:09:42.481Z",
  "channels": {
    "Stable": {
      "channel": "Stable",
      "version": "137.0.7151.55",
      "revision": "1453031",
      "downloads": {
        "chrome": [
          // ...略
        ],
        "chromedriver": [
          {
            "platform": "linux64",
            "url": "https://storage.googleapis.com/chrome-for-testing-public/137.0.7151.55/linux64/chromedriver-linux64.zip"
          },
          {
            "platform": "mac-arm64",
            "url": "https://storage.googleapis.com/chrome-for-testing-public/137.0.7151.55/mac-arm64/chromedriver-mac-arm64.zip"
          },
          {
            "platform": "mac-x64",
            "url": "https://storage.googleapis.com/chrome-for-testing-public/137.0.7151.55/mac-x64/chromedriver-mac-x64.zip"
          },
          {
            "platform": "win32",
            "url": "https://storage.googleapis.com/chrome-for-testing-public/137.0.7151.55/win32/chromedriver-win32.zip"
          },
          {
            "platform": "win64",
            "url": "https://storage.googleapis.com/chrome-for-testing-public/137.0.7151.55/win64/chromedriver-win64.zip"
          }
        ],
        "chrome-headless-shell": [
          // ...略
        ],
      }
    },
    "Beta": {
      // ...略
    },
    "Dev": {
      // ...略
    },
    "Canary": {
      // ...略
    }
  }
}

Jsonの.channels.Stable.downloads.chromedriver[]の配列の中で、platform="win64"のオブジェクトのurlを取得できればChrome driverバイナリのダウンロードURLが分かります。JsonをShellで解析するため、jqをインストールしてパスを通しておきます。今回の環境ではScoopというcommand-line installerを導入していたので、scoopでインストールしました。最近はwingetでもインストールできるようです。以下のjqコマンドで、ダウンロードURLを取得できます。

platform="win64"
curl https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json |
  jq -r ".channels.Stable.downloads.chromedriver[] | select(.platform == \"$platform\") | .url"

# https://storage.googleapis.com/chrome-for-testing-public/137.0.7151.55/win64/chromedriver-win64.zip が出力される

上記で取得したURLを変数latest_chromedriver_urlに格納し、さらにcurlでダウンロードします。

platform="win64"
latest_chromedriver_url=$(curl https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json |
  jq -r ".channels.Stable.downloads.chromedriver[] | select(.platform == \"$platform\") | .url")

curl -L -o "chromedriver.zip" "$latest_chromedriver_url"

Zipファイルを展開します。

unzip "chromedriver.zip"
# Archive:  chromedriver.zip
#   inflating: chromedriver-win64/LICENSE.chromedriver
#   inflating: chromedriver-win64/THIRD_PARTY_NOTICES.chromedriver
#   inflating: chromedriver-win64/chromedriver.exe

ls chromedriver-$platform/chromedriver.exe
# chromedriver-win64/chromedriver.exe
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?