LoginSignup
2
2

chromedriverをpowershellで自動更新する

Last updated at Posted at 2023-08-31

今までchromedriverの自動更新をネットで拾ってきたpowershellでやっていたのだが、chromedriverの更新方法が変わったので、それに対処した。
pythonを使っている人には本来必要ないようだけど、限界環境でその他のseleniumを使っている方、なるべく環境を変えたくない方向けに。日本であと2人くらいは困っている人がいるはず。
最初の3つのパラメータを自分の環境に合わせて変える必要がある。

# chrome実行ファイルの場所の設定
[CmdletBinding()]
param (
    [string]$ChromeDir="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
)

# chrome driverのディレクトリの相対指定
$chromeDriverRelativeDir = "..\..\AppData\Local\SeleniumBasic"

# chrome driverのplatformの設定
# win32/win64/linux64/mac-arm64/mac-x64のいずれか
$driverPlatform = "win32"

$thisScriptRoot = if ($PSScriptRoot -eq "") { "." } else { $PSScriptRoot }

$chromeDriverDir = $(Join-Path $thisScriptRoot $chromeDriverRelativeDir)
$chromeDriverFileLocation = $(Join-Path $chromeDriverDir "chromedriver.exe")
$chromeVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($ChromeDir).FileVersion
$chromeMajorVersion = $chromeVersion.split(".")[0]

if (-Not (Test-Path $ChromeDir -PathType Leaf)) {
  Write-Output "Chrome not found in '$ChromeDir'. Please, install chrome or specify custom chrome location with -ChromeDir argument."
  Pause
  Exit 1
}

if (-Not (Test-Path $chromeDriverDir -PathType Container)) {
  Write-Output "Chrome Driver folder '$chromeDriverDir' is not found. Please install selenium or specify selenium location."
  Pause
  Exit 1
#  New-Item -ItemType directory -Path $chromeDriverDir
}

if (Test-Path $chromeDriverFileLocation -PathType Leaf) {
  # get version of curent chromedriver.exe
  $chromeDriverFileVersion = (& $chromeDriverFileLocation --version)
  $chromeDriverFileVersionHasMatch = $chromeDriverFileVersion -match "ChromeDriver (\d+\.\d+\.\d+(\.\d+)?)"
  $chromeDriverCurrentVersion = $matches[1]

  if (-Not $chromeDriverFileVersionHasMatch) {
    Write-Output "failed to get version of chrome driver"
    Write-Output "update chrome driver"
    $chromeDriverCurrentVersion = ''
  }
}
else {
  # if chromedriver.exe not found, will download it
  $chromeDriverCurrentVersion = ''
}

# chrome-for-testingより、適切なバージョンの取得
$chromeDriverExpectedVersion = $chromeVersion.split(".")[0..2] -join "."
$chromeDriverJsonUrl = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
$json = Invoke-RestMethod -Uri $chromeDriverJsonUrl

$json_list = $json.versions | where{$_.version -match $chromeDriverExpectedVersion}
$json_proper_version = $json_list[$json_list.Length-1]
$chromeDriverProperVersion = $json_proper_version.version

$chromeDriverLatestVersion = $json.versions[$json.versions.Length-1].version

Write-Output "chrome version:        $chromeVersion"
Write-Output "chromedriver version : $chromeDriverCurrentVersion"
Write-Output "proper driver version: $chromeDriverProperVersion"
Write-Output "latest driver version: $chromeDriverLatestVersion"

# will update chromedriver.exe if old patch

$needUpdateChromeDriver = $chromeDriverCurrentVersion -ne $chromeDriverProperVersion
if ($needUpdateChromeDriver) {
  $json_driver = $json_proper_version.downloads.chromedriver | where{$_.platform -eq $driverPlatform}
  $chromeDriverZipLink = $json_driver.url
  Write-Output "Will download $chromeDriverZipLink"

  $driverZipFileName = $chromeDriverZipLink.split("/")[-1]
  $chromeDriverZipFileLocation = $(Join-Path $chromeDriverDir $driverZipFileName)
  $driverFileName = $driverZipFileName.split(".")[0]

  Invoke-WebRequest -Uri $chromeDriverZipLink -OutFile $chromeDriverZipFileLocation
  Expand-Archive $chromeDriverZipFileLocation -DestinationPath $chromeDriverDir -Force
  Move-Item $(Join-Path $chromeDriverDir $driverFileName | join-path -ChildPath "\chromedriver.exe") $chromeDriverFileLocation -Force
  Remove-Item -Path $chromeDriverZipFileLocation -Force
  Remove-Item -Path $(Join-Path $chromeDriverDir $driverFileName) -Recurse -Force
  $chromeDriverFileVersion = (& $chromeDriverFileLocation --version)
  Write-Output "chromedriver updated to version $chromeDriverFileVersion"
}
else {
  Write-Output "chromedriver is actual"
}

ちなみにchromeの自動更新を止めるには
C:\Program Files (x86)\Google\Update\GoogleUpdate.exe
のファイル名を変えると、無理やり止められるようだ。

2
2
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
2
2