2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShellで使用しているSeleniumとWebDriverを日々アップデートする仕組みを作成

Posted at

1. Seleniumライブラリのアップデート

Seleniumの.NETライブラリをNuGetから取得している場合は、以下のスクリプトを使用して最新バージョンを取得できます。

# NuGet CLIを使用してSelenium.WebDriverをアップデート
nuget update Selenium.WebDriver -Source "https://api.nuget.org/v3/index.json"

# 必要に応じて他の関連ライブラリも更新
nuget update Selenium.Support -Source "https://api.nuget.org/v3/index.json"

2. WebDriverのアップデート

ブラウザごとのWebDriverを最新に保つためには、以下の方法を使用します。

2.1 手動ダウンロードを自動化するスクリプト
以下はChromeDriverを例にしたアップデートスクリプトです。ブラウザのバージョンに応じて適切なバージョンのWebDriverをダウンロードします。

UpdateSelenium.ps1
# Google Chromeのバージョン取得
$chromeVersion = (Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome").DisplayVersion

# ChromeDriverの最新バージョン取得
$driverVersionUrl = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$chromeVersion"
$latestDriverVersion = Invoke-RestMethod -Uri $driverVersionUrl

# ダウンロード先URL
$driverDownloadUrl = "https://chromedriver.storage.googleapis.com/$latestDriverVersion/chromedriver_win32.zip"

# ダウンロード先ファイルパス
$downloadPath = "$env:TEMP\chromedriver.zip"
Invoke-WebRequest -Uri $driverDownloadUrl -OutFile $downloadPath

# 古いドライバを削除し、新しいものを解凍
$driverPath = "C:\Path\To\Your\Drivers\"
Remove-Item -Path "$driverPath\chromedriver.exe" -Force
Expand-Archive -Path $downloadPath -DestinationPath $driverPath -Force

Write-Host "ChromeDriver updated to version $latestDriverVersion"
他のブラウザ(FirefoxGeckoDriverEdgeEdgeDriver)の場合も同様の仕組みを使えますが、対応するURLを変更してください。

3. スケジュールタスクを設定する

日々の自動実行には、Windowsのスケジュールタスクを設定します。

スクリプト保存: 上記スクリプトをPowerShellファイル(例: UpdateSelenium.ps1)として保存します。

スケジュールタスク作成: 以下の手順でタスクを設定します。

タスクスケジューラを開く。
「基本タスクの作成」を選択。
タスク名を入力(例: Update Selenium and Drivers)。
実行頻度を選択(例: 毎日)。
実行するプログラムに以下を入力:

StartUpdate.bat
powershell.exe -ExecutionPolicy Bypass -File "C:\Path\To\UpdateSelenium.ps1"
  1. 確認とログの記録
    アップデートが成功したかを確認するため、ログを記録する仕組みを追加すると便利です。以下はログを記録する例です。
UpdateCheck.ps1
# ログファイルパス
$logFile = "C:\Path\To\Logs\UpdateLog.txt"

# ログに書き込む関数
function Write-Log {
    param([string]$message)
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    Add-Content -Path $logFile -Value "$timestamp - $message"
}

# ログの開始
Write-Log "Starting Selenium and WebDriver update."

# Seleniumアップデート
try {
    nuget update Selenium.WebDriver -Source "https://api.nuget.org/v3/index.json"
    Write-Log "Selenium.WebDriver updated successfully."
} catch {
    Write-Log "Error updating Selenium.WebDriver: $_"
}

# WebDriverアップデート(例: ChromeDriver)
try {
    # ChromeDriver更新スクリプトの内容
    Write-Log "ChromeDriver updated successfully."
} catch {
    Write-Log "Error updating ChromeDriver: $_"
}

Write-Log "Update completed."
2
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?