VBA:【Selenium×Edge】ブラウザのドライバ(webdriver、edgedriver)のバージョンチェックと自動更新用VBAコード
「Selenium」を導入しようとして、ブラウザのドライバのバージョン関連で困った点。
◆Edge版
Edgeでの依頼だったので、ひとまずEdgeで動作するように作っています。
エラー処理などは入れていないので、使う際にはエラー処理や条件など追加して使ってください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
Option Compare Database 'Windows APIの宣言' Private Declare PtrSafe Function URLDownloadToFile _ Lib "urlmon" Alias "URLDownloadToFileA" _ (ByVal pCaller As Long, _ ByVal szURL As String, _ ByVal szFileName As String, _ ByVal dwReserved As Long, _ ByVal lpfnCB As Long) As Long '#######################################################' Private Sub chkEdgeVersion_Click() Dim BrowserVer As String Dim thisDriverVer As String Dim xTmpPath As String Dim DownloadFileName As String Dim SeleniumDriverPath As String Dim uName As String xTmpPath = CurrentProject.Path & "\xTmp" DownloadFileName = xTmpPath & "\edgedriver_win64.zip" '保存先をデフォルトから変更している方は変更してください。' uName = Environ("USERNAME") SeleniumDriverPath = "C:\Users\" & uName & "\AppData\Local\SeleniumBasic\edgedriver.exe" '" 'フォルダの有無の確認。なければフォルダを作成する' If Dir(xTmpPath, vbDirectory) = "" Then MkDir xTmpPath End If 'PCのブラウザのバージョンをチェック' BrowserVer = chkBrowserVer 'Seleniumで保存しているドライバのバージョンチェック' If Dir(SeleniumDriverPath) = "" Then 'ない場合はチェックせず、新しいファイルを書き込む' Else thisDriverVer = chkDriverVer(SeleniumDriverPath) End If If BrowserVer <> thisDriverVer Then 'ブラウザバージョンと一致したドライバのダウンロード' Call DriverDownload(BrowserVer, DownloadFileName) 'ダウンロードしたファイルを解凍して所定の場所にドライバを保存' Call DriverUpdate(DownloadFileName, xTmpPath, SeleniumDriverPath) Else MsgBox "バージョン一致" '何もしない' End If MsgBox "完了" End Sub '#######################################################' '//【Edge】ブラウザのバージョンチェックします' Function chkBrowserVer() As String Dim chkVer As String 'Edgeの場合' chkVer = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge\BLBeacon\version" chkBrowserVer = CreateObject("WScript.Shell").RegRead(chkVer) End Function '#######################################################' '//現在SeleniumBasicフォルダに保存されているドライバのバージョンをチェックします' Function chkDriverVer(SeleniumDriverPath As String) As String Dim FSO As Object Set FSO = CreateObject("Scripting.FileSystemObject") chkDriverVer = FSO.GetFileVersion(FileName:=SeleniumDriverPath) End Function '#######################################################' '//ドライバをダウンロードします' Public Sub DriverDownload(BrowserVer As String, dFileName As String) Dim dURL As String Dim dResult As String 'ダウンロードするファイルのURL' dURL = "https://msedgedriver.azureedge.net/" & BrowserVer & "/edgedriver_win64.zip" 'ダウンロード' dResult = URLDownloadToFile(0, dURL, dFileName, 0, 0) End Sub '#######################################################' '//ドライバを解凍して特定の場所に保存します。' Public Sub DriverUpdate(DownloadFileName As String, xTmpPath As String, SeleniumDriverPath As String) Dim psCommand As String Dim wsh As Object Dim dResult As String '//実行するPowerShellのコマンド作成。「 -Force」で上書き。' psCommand = "powershell -NoProfile -ExecutionPolicy Unrestricted Expand-Archive -Path " & DownloadFileName & " -DestinationPath " & xTmpPath & " -Force" Set wsh = CreateObject("WScript.Shell") '//PowerShellのコマンド実行' '指定した場所に解凍されます。' dResult = wsh.Run(Command:=psCommand, WindowStyle:=0, WaitOnReturn:=True) '解凍したexeファイルをSeleniumのドライバに上書き' FileCopy xTmpPath & "\msedgedriver.exe", SeleniumDriverPath Set wsh = Nothing End Sub |
◆Egde版

VBA:【Selenium×Chrome】ブラウザのドライバ(WebDriver、chromedriver)のバージョンチェックと自動更新
【VBA×Selenium】Chrome:ブラウザのドライバ(WebDriver・chromedriver)とSelenium Basicのブラウザのドライバのバージョンを比較して、異なるバージョンの場合は上書きする方法について