SANANEBLOG
PowerShell PR

【Version 115以降対応】PowerShellを使用したChromeDriverの自動インストールスクリプト

記事内に商品プロモーションを含む場合があります

はじめに

今回の記事ではChrome ver115以降に対応したPowerShellを使用してChromeDriverを自動的に更新するスクリプトの作成方法を紹介します。

公式サイトによると、ver115以降のChromeDriverは、Chrome for Testingから取得する仕様に変更になりました。

ver115以降のChromeDriverをダウンロードするためのJSONエンドポイントが変更になったため、以前から使用していたChromeDriverの自動アップデートスクリプトが動作しなくなってしまいました。

そのため、今回は修正したChrome ver115以降に対応したスクリプトを紹介します。

スクリプト

# エラー発生時にスクリプトを停止する設定
$ErrorActionPreference = "Stop"

# ChromeDriverの保存先ディレクトリのパスを設定
$chromeDriverDirectoryPath = Join-Path $PSScriptRoot drivers

# Chromeのインストール先のパスを設定
$chromeInstallPath32bit = "C:\Program Files (x86)\Google\Chrome\Application"
$chromeInstallPath64bit = "C:\Program Files\Google\Chrome\Application"

# Chromeのインストール先のパスを確認
if (Test-Path -Path $chromeInstallPath32bit){
   $chromeInstallPath = $chromeInstallPath32bit
} else {
   $chromeInstallPath = $chromeInstallPath64bit
}

# Chromeのバージョンを取得
$chromeVersion = (Get-Childitem "$chromeInstallPath\[0-9]*" -Directory -Name).split(".")[0]

# ChromeDriverのバージョンを取得(存在しない場合は$nullを設定)
$driverVersion = if (Test-Path "$chromeDriverDirectoryPath\chromedriver.exe") {
    (Invoke-Expression "$chromeDriverDirectoryPath/chromedriver --version").split(" ")[1].split(".")[0]
} else {
    $null
}

# バージョン情報を出力
echo "Browser Version: $chromeVersion"
echo "Driver Version: $driverVersion"

# ChromeのバージョンとChromeDriverのバージョンが異なる場合、またはChromeDriverが存在しない場合、更新処理を実行
if($chromeVersion -ne $driverVersion -or $driverVersion -eq $null){
   try {
       # ChromeDriverのダウンロードURLを取得
       $downloadURL = (Invoke-Webrequest -Uri "https://googlechromelabs.github.io/chrome-for-testing/" -UseBasicParsing |
               Select-String -Pattern "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/$chromeVersion\.[0-9]+\.[0-9]+\.[0-9]+/win32/chromedriver-win32.zip" |
               ForEach-Object { $_.Matches.Value })

       # URLが取得できない場合、エラーをスロー
       if (-not $downloadURL) {
           throw "Unable to find the download link for ChromeDriver version $chromeVersion"
       }

       # 一時ディレクトリに移動して、ChromeDriverをダウンロード
       cd "$env:TEMP"
       Invoke-Webrequest -Uri $downloadURL -OutFile "chromedriver_win32.zip" -UseBasicParsing

       # ZIPファイルを展開
       expand-archive -Path chromedriver_win32.zip -DestinationPath "chromedriver-win32" -Force

       # 正しいサブディレクトリからchromedriver.exeを移動
       Move-Item "chromedriver-win32\chromedriver-win32\chromedriver.exe" "$chromeDriverDirectoryPath" -Force

       # 一時ファイルとディレクトリを削除
       Remove-Item "chromedriver-win32" -Recurse -Force
       Remove-Item "chromedriver_win32.zip" -Force
   } catch {
       # エラー情報を出力
       Write-Error $_.Exception.Message
   }
}

実行方法

  1. [任意のファイル名].ps1として上記のスクリプトを記載したファイルを作成します。
  2. ①と同じフォルダに[drivers]というフォルダを作成します。
  3. [任意のファイル名].ps1を右クリックして、[PowerShellで実行]をクリックします。
  4. PowerShellが実行され、問題なく完了するとウィンドウが閉じます。
  5. [drivers]フォルダにChromeと同バージョンのchromedriver.exeが保存されます。

タスクスケジューラに組み込むことや、他のPowerShellスクリプトに組み込むことも可能です。

スクリプトの動作

  1. インストールされているGoogle Chromeのバージョンを取得します。
  2. 既に保存されているChromeDriverのバージョンを取得します。
  3. 両者のバージョンが異なる、またはChromeDriverが存在しない場合、新しいChromeDriverをダウンロードします。
  4. ダウンロードしたZIPファイルを展開し、指定したディレクトリに保存します。

まとめ

Google ChromeやChromeDriverの更新は頻繁に行われるため、手動での管理は大変です。

このスクリプトを使用することで、PowerShellを使用したブラウザ自動化やスクレイピングなどがより効率的になります。

参考になれば幸いです。