【PowerShell】Selenium-Module ログオフ状態の動作検証

目次

概要

 Seleniumでスクレイピングをする時、各ブラウザ(Chrome、Edge等)のWebドライバーを介してブラウザ操作を行います。Chrome等のブラウザのアップデートに合わせてWebドライバーも更新しなければなりません。
 PowerShellのSelenium-ModuleにおいてもWebドライバーを更新しなければなりませんが、Selenium-ModuleではWebドライバーを更新してくれるスクリプトが事前に用意してくれています。今回は、スクリプトの実行方法の紹介と、自動実行するための設定方法を紹介していきます。
 ちなみにWebドライバーは以下フォルダにて、「Selenium-Binary-Updater.ps1」というファイル名で保存されています。

PowerShell(Ver7)

C:\Users\<ユーザ名>\Documents\PowerShell\Modules\Selenium\3.0.1\Selenium-Binary-Updater.ps1

WindowsPowerShell

C:\Program Files\WindowsPowerShell\Modules\Selenium\3.0.1\Selenium-Binary-Updater.ps1

Example

 今回はChromeドライバーを更新するスクリプトを作成しました。上記ファイルを引数でChrome指定して呼び出すスクリプトになります。

$shell = New-Object -ComObject Shell.Application
$DocumentDir = $shell.NameSpace("shell:Personal").Self.Path
Set-Location $DocumentDir"\PowerShell\Modules\Selenium\3.0.1"
. ./Selenium-Binary-Updater.ps1 -Browser "Chrome"

自動更新設定

 上記スクリプトを定期的すれば手動で呼び出す必要もなく、気付かない間にドライバーが自動的に更新してくれます。今回は、PC起動時に自動的に更新する方法タスクスケジューラで定期実行する方法を紹介します。
 上記PowerShellのコードは“C:\TaskScheduler\selenium_module\selenium_module_update.ps1”で保存しておきます。

PC起動時の実行(スタートアップフォルダ保存)

 PC起動時に自動的にChromeドライバーを更新する簡易的な方法として、実行ファイルをスタートアップフォルダに保存しておく方法を紹介します。
 上記PowerShellのコードは“C:\TaskScheduler\selenium_module\selenium_module_update.ps1”で保存しておきます。

Powershell実行ファイルの作成

 Powershellファイルを実行するためのショートカットファイルを作成します。

スタートアップフォルダに保存

タスクスケジューラによる定期実行

 PC起動時の更新実行する方法を前段で紹介しましたが、PC起動時に毎回更新実行をしたくない方については、タスクスケジューラで更新実行の頻度を調整することができます。しかし、タスクスケジューラで実行する場合は実行時にPCが起動している必要があります。
 タスクスケジューラの定期実行の方法は簡易的に説明します。以下設定にてタスクスケジューラを設定します。

  • トリガーの設定
    週一回で設定、停止するまでの時間10分
  • 操作の設定
    プログラム/スクリプト pwsh
    引数の追加 -Command “C:\TaskScheduler\selenium_module\New-SeScreenshot.ps1”
    開始(オプション) C:\TaskScheduler\selenium_module

タスクスケジューラの詳細な設定方法については以下ページを参照してみてください。

なお、PCがスリープしているログオフ状態でもタスク実行をしたい方は以下ページを参照してみてください。

Module

Selenium-Binary-Updater.ps1の構文を以下に掲載します。

param(
    [Parameter(Mandatory=$true)][ValidateSet('Chrome','Firefox','Edge')]$Browser
)

$TempDir = [System.IO.Path]::GetTempPath()

switch ($Browser){
    'Chrome'{
        $LatestChromeStableRelease = Invoke-WebRequest 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE' | Select-Object -ExpandProperty Content
        $ChromeBuilds = @('chromedriver_linux64','chromedriver_mac64','chromedriver_win32')
        
        foreach ($Build in $ChromeBuilds){
            switch($Build){
                'chromedriver_linux64'{
                    $AssembliesDir = "$PSScriptRoot/assemblies/linux"
                    $BinaryFileName = 'chromedriver'
                    }
                'chromedriver_mac64'{
                    $AssembliesDir = "$PSScriptRoot/assemblies/macos"
                    $BinaryFileName = 'chromedriver'
                    }
                'chromedriver_win32'{
                    $AssembliesDir = "$PSScriptRoot/assemblies"
                    $BinaryFileName = 'chromedriver.exe'
                    }
                default{throw 'Incorrect Build Type'}
            }
        
            $BuildFileName = "$Build.zip"
            Write-Verbose "Downloading: $BuildFileName"
            Invoke-WebRequest -OutFile "$($TempDir + $BuildFileName)" "https://chromedriver.storage.googleapis.com/$LatestChromeStableRelease/$BuildFileName" 
            
            # Expand the ZIP Archive to the correct Assemblies Dir 
            Write-Verbose "Explanding: $($TempDir + $BuildFileName) to $AssembliesDir"
            Expand-Archive -Path "$($TempDir + $BuildFileName)" -DestinationPath $AssembliesDir -Force
            
            # Generate Hash Files 
            Write-Verbose "Generating SHA256 Hash File: $AssembliesDir/$BinaryFileName.sha256"
            Get-FileHash -Path "$AssembliesDir/$BinaryFileName"  -Algorithm SHA256 | Select-Object -ExpandProperty Hash | Set-Content -Path "$AssembliesDir/$BinaryFileName.sha256" -Force
            
        }
    }
    'Firefox'{
        Write-Host 'Not Supported Yet' 

    }
    'Edge'{
        Write-Host 'Not Supported Yet' 
    }
}
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

VBAを中心とした自動化、効率化の手法を紹介しています。現在は、SeleniumBasicのexamplesを紹介しています。その内、SeleniumBasic以外の手法も掲載したいと思っております。

目次