SeleniumBasic WaiterClass 待機用関数

リファレンス

概要

待機用の関数クラスを解説します。一部メソッドは、64bit版Officeで動作不能になります。

オブジェクト設定

・変数宣言

Dim Waiter As New Waiter

文法

Waiter.[メソッド]

メソッド・プロパティ

メソッド

スクロールできます
Name解説ShortExample
Not引数がFalseである状態がタイムアウトに達したらエラー発生While Waiter.Not(WaitDelegate1(), Timeout:=2000): Wend
Until< T> デリゲート関数のnot null or true の返りを待機。64bit版Officeでは動作不能Waiter.Until AddressOf WaitDelegate2, Timeout:=2000
Wait指定のミリ秒待機Waiter.Wait 2000
WaitForFileファイルの準備ができるまで待機。Waiter.WaitForFile FullPath, 50

※Short Exampleは、動作確認ができたコードを記載しています。

プロパティ

スクロールできます
Name解説ShortExample
Timeout待機タイムアウト (ミリ秒)。デフォルトは 5秒。NotメソッドとUntilメソッドで利用Waiter.Timeout = 3000

※Short Exampleは、動作確認ができたコードを記載しています。

Example

Should_Wait_For_Delegate

Private Sub Should_Wait_For_Delegate()
    Dim Driver As New ChromeDriver
    Dim Waiter As New Waiter

    Waiter.Timeout = 3000
    'Step1 delegate無し 引数無し
    While Waiter.Not(WaitDelegate1(), Timeout:=2000): Wend
    
    'Step2 delegate無し 引数有り
    While Waiter.Not(WaitDelegate2(Driver)): Wend
    
    'Step3 delegate有り 引数無し
    Waiter.Until AddressOf WaitDelegate2, Timeout:=2000
    
    'Step4 delegate有り 引数有り
    Waiter.Until AddressOf WaitDelegate1, Driver, Timeout:=2000
    
    'Step5 ChromeDriverクラスのUntilメソッド利用(delegate有り 引数無し)
    Driver.Until AddressOf WaitDelegate1, Timeout:=2000

    'Step6 ChromeDriverクラスのUntilメソッド利用(delegate有り 引数有り)
    Driver.Until AddressOf WaitDelegate1, Driver, Timeout:=2000
End Sub

Private Function WaitDelegate1()
    WaitDelegate1 = False
End Function

Private Function WaitDelegate2(Driver As WebDriver)
    WaitDelegate2 = False
End Function

Download_File_Chrome

Private Sub Download_File_Chrome()
    Dim Driver As New ChromeDriver
    Dim elm As Selenium.WebElement
    Dim Waiter As New Selenium.Waiter

    'Chromeの環境設定
    Driver.SetPreference "download.default_directory", ThisWorkbook.Path
    Driver.SetPreference "download.directory_upgrade", True
    Driver.SetPreference "download.prompt_for_download", False
  Driver.SetPreference "plugins.always_open_pdf_externally", True
    
    'ダウンロード作業前の準備
    Driver.Get "https://www.jka.or.jp/jka-news-pdf/jka-no34-pdf10mb/"
    Dim Keys As New Selenium.Keys
    Set elm = Driver.FindElementByPartialLinkText("JKAニュース No34")
    Dim buf, FileName As String
    buf = Split(elm.Attribute("href"), "/")
    FileName = buf(UBound(buf))

    Dim FullPath As String
    FullPath = ThisWorkbook.Path & "\" & FileName
    If FileName Like "*.pdf" And Dir(FullPath) = "" Then
        Driver.Actions.MoveToElement(elm).Perform
        Waiter.Wait 2000
        elm.Click Keys.Alt

ErrResume:
        On Error GoTo ErrHandle
        Waiter.WaitForFile FullPath, 50
    End If
    Driver.Quit
    Exit Sub

ErrHandle:
Resume ErrResume
End Sub
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

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