Chrome浏览器更新时更新chromedriver

时间:2018-04-05 14:33:31

标签: google-chrome browser selenium-chromedriver

每当Chrome浏览器更新时,是否有必要更新chromedriver(用于自动化机器人)?或者机器人可以使用较旧版本的chromedriver和较新版本的浏览器运行良好?

2 个答案:

答案 0 :(得分:0)

并非总是如此,但当版本不兼容时,答案是肯定的。

您可以在此处查看chromedriver版本列表及其支持的浏览器版本:

https://chromedriver.storage.googleapis.com/2.26/notes.txt

希望这有帮助

答案 1 :(得分:0)

是的,只要chromedriver浏览器更新,就必须更新chrome。 您可以在此处找到选择chromedriver版本的指南:https://sites.google.com/a/chromium.org/chromedriver/downloads/version-selection

最近,我发现chromedriver几乎符合一对一的版本要求(chromedriver 2.46的支持最新版本为三)。

我已经wrote simple powershell script用于自动更新chromedriver(在Windows平台上)。

在需要更新crhomedriver时运行此脚本(在运行硒Web测试之前,我在CI中将此脚本作为构建步骤运行):

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

$chromeDriverRelativeDir = "Selenium"
$chromeDriverDir = $(Join-Path $thisScriptRoot $chromeDriverRelativeDir)
$chromeDriverFileLocation = $(Join-Path $chromeDriverDir "chromedriver.exe")
$chromeVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe").FileVersion
$chromeMajorVersion = $chromeVersion.split(".")[0]

if (-Not (Test-Path $chromeDriverDir -PathType Container)) {
  $dir = 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"
}

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

希望这会有所帮助。