如何为 windows 10 更新 chrome 驱动程序

How to update chrome driver for windows 10

您好,我想将我的 chrome 驱动程序更新到最新版本,但找不到任何关于更新驱动程序的信息,只是关于安装它的信息。我需要做什么才能将驱动程序更新到最新版本?

chromedriver 是一个独立的可执行文件。只需将您现有的版本替换为较新的版本即可。

  1. https://sites.google.com/a/chromium.org/chromedriver/downloads
  2. 下载最新版本的chromedriver_win32.zip
  3. 解压缩文件以提取chromedriver.exe
  4. 用这个新的可执行文件替换您现有的文件。

最近我发现 chromedriver 现在几乎是一对一的版本一致性(chromedriver 2.46 有 three-last-releases 支持政策)。请在此处查看版本选择指南:https://sites.google.com/a/chromium.org/chromedriver/downloads/version-selection

我已经 wrote simple powershell script 自动更新 chromedriver

运行 当你需要更新这个脚本时 crhomedriver (就像 CI/CD 中的构建步骤,就在 运行 selenium web 测试之前):

[CmdletBinding()]
param (
    [string]$ChromeDir="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
)

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."
  Exit 1
}

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

$chromeDriverRelativeDir = "Selenium"
$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 $chromeDriverDir -PathType Container)) {
  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) {
    Exit
  }
}
else {
  # if chromedriver.exe not found, will download it
  $chromeDriverCurrentVersion = ''
}

if ($chromeMajorVersion -lt 73) {
  # for chrome versions < 73 will use chromedriver v2.46 (which supports chrome v71-73)
  $chromeDriverExpectedVersion = "2.46"
  $chromeDriverVersionUrl = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE"
}
else {
  $chromeDriverExpectedVersion = $chromeVersion.split(".")[0..2] -join "."
  $chromeDriverVersionUrl = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_" + $chromeDriverExpectedVersion
}

$chromeDriverLatestVersion = Invoke-RestMethod -Uri $chromeDriverVersionUrl

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

# will update chromedriver.exe if MAJOR.MINOR.PATCH
$needUpdateChromeDriver = $chromeDriverCurrentVersion -ne $chromeDriverLatestVersion
if ($needUpdateChromeDriver) {
  $chromeDriverZipLink = "https://chromedriver.storage.googleapis.com/" + $chromeDriverLatestVersion + "/chromedriver_win32.zip"
  Write-Output "Will download $chromeDriverZipLink"

  $chromeDriverZipFileLocation = $(Join-Path $chromeDriverDir "chromedriver_win32.zip")

  Invoke-WebRequest -Uri $chromeDriverZipLink -OutFile $chromeDriverZipFileLocation
  Expand-Archive $chromeDriverZipFileLocation -DestinationPath $(Join-Path $thisScriptRoot $chromeDriverRelativeDir) -Force
  Remove-Item -Path $chromeDriverZipFileLocation -Force
  $chromeDriverFileVersion = (& $chromeDriverFileLocation --version)
  Write-Output "chromedriver updated to version $chromeDriverFileVersion"
}
else {
  Write-Output "chromedriver is actual"
}

您可以使用 $chromeDriverRelativeDir 变量配置需要放置 chromedriver 的相对目录,相对于脚本位置。