【VBA】SeleniumUtil

準備

SeleniumBasicのインストール

Seleniumbasic
f:id:vist764:20210822225546p:plain:w400
f:id:vist764:20210822225725p:plain:w400

driver.exeの設置(Chromeの場合)

1.バージョンを確認する
f:id:vist764:20210822230128p:plain
2.バージョンに合ったexeファイルをDLする
※バージョンが合わないと動かない
Chrome
Downloads - ChromeDriver - WebDriver for Chrome
【Edge】
Microsoft Edge ドライバー - Microsoft Edge Developer
f:id:vist764:20210822230258p:plain:w400
f:id:vist764:20210822230336p:plain:w400
3.SeleniumBasicのフォルダにexeファイルを入れる(上書き)
f:id:vist764:20210822230651p:plain:w400
Microsoft Edge ドライバー - Microsoft Edge Developer

.NET Frameworkをインストールする

SeleniumBasicのフォルダ内のScriptsの
Start~.vbsを実行する
f:id:vist764:20210822233421p:plain:w400
f:id:vist764:20210822233607p:plain:w400

参照設定を行う

f:id:vist764:20210822232225p:plain:w300

ソース

Option Explicit

'v1
'****************************************************
'Selenium操作処理
'----------------------------------------------------
'SeleniumUtil
'----------------------------------------------------
'参照設定
'   Selenium Type Library
'   ※Selenium Basicのインストール、Driverの設置が必要
'----------------------------------------------------
'備考
'   Dim driver As New Selenium.ChromeDriver   -宣言(Chromeの例)
'****************************************************
'OpenURL             -URLを開く
'GoLink              -リンクを開く
'GoBack              -前のページに戻る
'GoForward           -次のページに進む
'Refresh             -画面を再表示する
'CloseBrowser        -IEを閉じる
'SwitchTab           -操作タブを切り替える
'FindElementByTag    -要素を取得(タグ+文字列指定)
'****************************************************

Const ウェイト = 3  '待機する秒数(0で待機しない)

Public Enum enmプロパティ
    innerText
    outerText
    innerHTML
    outerHTML
End Enum

'----------------------------------------------------
'■URLを開く
'----------------------------------------------------
'引数1:対象のオブジェクト
'引数2:開くURL
'引数3:(Optional)画面非表示にするか ※Trueなら非表示 デフォルトは非表示にしない
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function OpenURL(aDriver As Object, aURL As String, Optional aIsInvisible As Boolean) As Boolean
On Error GoTo Err
    '画面表示しない場合
    If aIsInvisible Then aDriver.AddArgument "headless"
    'URLを開く
    aDriver.Start
    aDriver.Get aURL
    '待つ
    If ウェイト <> 0 Then aDriver.Wait ウェイト * 1000
    OpenURL = True
    If logging Then AddLog "SeleniumUtil.OpenURL", "INFO ", "URL:" & aURL & " / タイトル:" & aDriver.title
    Exit Function
Err:
    If logging Then AddLog "SeleniumUtil.OpenURL", "ERROR", "URL:" & aURL & " / エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■リンクを開く
'----------------------------------------------------
'引数1:対象のオブジェクト
'引数2:リンクテキスト
'引数3:(Optional)部分一致でリンクテキストを検索するか ※Trueで部分一致 デフォルトは完全一致
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function GoLink(aDriver As Object, aLinkText As String, Optional aIsPartialMatch As Boolean) As Boolean
On Error GoTo Err
    '部分一致
    If aIsPartialMatch Then
        aDriver.FindElementByPartialLinkText(aLinkText).Click
    Else
    '完全一致
        aDriver.FindElementByLinkText(aLinkText).Click
    End If
    '待つ
    If ウェイト <> 0 Then aDriver.Wait ウェイト * 1000
    GoLink = True
    If logging Then AddLog "SeleniumUtil.GoLink", "INFO ", "リンクテキスト:" & aLinkText & " / リンク先:" & aDriver.url
    Exit Function
Err:
    If logging Then AddLog "SeleniumUtil.GoLink", "ERROR", "リンクテキスト:" & aLinkText & " / エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■前のページに戻る
'----------------------------------------------------
'引数:対象のIEオブジェクト
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function GoBack(aDriver As Object) As Boolean
On Error GoTo Err
    '前のページに戻る
    aDriver.GoBack
    '待つ
    If ウェイト <> 0 Then aDriver.Wait ウェイト * 1000
    GoBack = True
    If logging Then AddLog "SeleniumUtil.GoBack", "INFO ", "タイトル:" & aDriver.title
    Exit Function
Err:
    If logging Then AddLog "SeleniumUtil.GoBack", "ERROR", "エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■次のページに進む
'----------------------------------------------------
'引数:対象のオブジェクト
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function GoForward(aDriver As Object) As Boolean
On Error GoTo Err
    '次のページに進む
    aDriver.GoForward
    '待つ
    If ウェイト <> 0 Then aDriver.Wait ウェイト * 1000
    GoForward = True
    If logging Then AddLog "SeleniumUtil.GoForward", "INFO ", "タイトル:" & aDriver.title
    Exit Function
Err:
    If logging Then AddLog "SeleniumUtil.GoForward", "ERROR", "エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■画面を再表示する
'----------------------------------------------------
'引数:対象のオブジェクト
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function Refresh(aDriver As Object) As Boolean
On Error GoTo Err
    '再表示
    aDriver.Refresh
    '待つ
    If ウェイト <> 0 Then aDriver.Wait ウェイト * 1000
    Refresh = True
    If logging Then AddLog "SeleniumUtil.Refresh", "INFO ", "タイトル:" & aDriver.title
    Exit Function
Err:
    If logging Then AddLog "SeleniumUtil.Refresh", "ERROR", "エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■ブラウザを閉じる
'----------------------------------------------------
'引数:対象のオブジェクト
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function CloseBrowser(aDriver As Object) As Boolean
On Error GoTo Err
    '閉じる
    aDriver.Quit
    Set aDriver = Nothing
    CloseBrowser = True
    If logging Then AddLog "SeleniumUtil.CloseBrowser", "INFO "
    Exit Function
Err:
    If logging Then AddLog "SeleniumUtil.CloseBrowser", "ERROR", "エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■操作タブを切り替える
'----------------------------------------------------
'引数1:対象のオブジェクト
'引数2:(Optional)切り替えるタブ名
'----------------------------------------------------
'戻り値:実行結果 ※エラーがなければTrue
'----------------------------------------------------
Public Function SwitchTab(aDriver As Object, Optional aTitle As String) As Boolean
On Error GoTo Err
    '切り替える
    '(タブ名を指定あり)
    If aTitle <> "" Then aDriver.SwitchToWindowByTitle aTitle
    '(タブ名を指定なし)
    If Not aTitle <> "" Then aDriver.SwitchToNextWindow
    SwitchTab = True
    If logging Then AddLog "SeleniumUtil.SwitchTab", "INFO ", "タイトル:" & aDriver.title
    Exit Function
Err:
    If logging Then AddLog "SeleniumUtil.SwitchTab", "ERROR", "エラー内容:" & Err.Description
End Function

'----------------------------------------------------
'■要素を取得(タグから+文字列指定)
'----------------------------------------------------
'引数1:対象のオブジェクト
'引数2:要素名
'引数3:検索文字
'引数4:(Optional)検索する箇所 ※デフォルトはouterHTML
'----------------------------------------------------
'戻り値:取得結果
'----------------------------------------------------
Public Function FindElementByTag(aDriver As Object, aElementName As String, aString As String, Optional aProperty As enmプロパティ = outerHTML) As Object
    Dim objTag As Object
    Dim propertyName As String
    Select Case aProperty
        Case enmプロパティ.outerHTML: propertyName = "outerHTML"
        Case enmプロパティ.innerHTML: propertyName = "innerHTML"
        Case enmプロパティ.outerText: propertyName = "outerText"
        Case enmプロパティ.innerText: propertyName = "innerText"
    End Select
    
    For Each objTag In aDriver.FindElementsByTag(aElementName)
        If InStr(objTag.Attribute(propertyName), aString) > 0 Then
            Set FindElementByTag = objTag
            Exit Function
        End If
    Next
End Function

機能

    '宣言
    Dim driver As New Selenium.ChromeDriver

    '要素を取得 ※添え字は1から
    Set obj = driver.FindElementById("id属性名")
    Set obj = driver.FindElementsByName("name属性名")(1)
    Set obj = driver.FindElementsByClass("class属性名")(1)
    Set obj = driver.FindElementsByTag("tag属性名")(1)
    Set obj = driver.FindElementByCss("cssセレクター")
    
    '全要素を取得
    Dim elements As WebElements
    Set elements = driver.FindElementsByTag("a")
    For Each element In elements
        Debug.Print element.Attribute("href")
        Debug.Print element.Attribute("outerHTML")
    Next
    
    'ソースを取得
    Str = driver.PageSource
    
    'ウェイト ※1000で1秒
    driver.Wait 1000
    
    '存在確認
    Dim by As New by
    bool = driver.IsElementPresent(by.ID("属性名"))
    
    '文字入力 ※クリアしておかなければ追記される
    driver.FindElementById("id属性名").Clear
    driver.FindElementById("id属性名").SendKeys "テスト"
    
    'ボタン押下
    driver.FindElementById("id属性名").Click
    
    '画面サイズ指定 ※タテ、ヨコ
    driver.Window.SetSize 1500, 1000
    
    '画面サイズ最大
    driver.Window.Maximize
    
    'スクリーンショット ※拡張子(png,jpg)必要
    driver.TakeScreenshot.SaveAs "フルパスorファイル名"
    
    'タイトル
    Str = driver.Title
    
    'URL
    Str = driver.URL

備考

f:id:vist764:20210820141554p:plain:w400