SeleniumBasic ApplicationClass 代表的なクラス群の呼出し

リファレンス

概要

SeleniumBasicの代表的なクラスをまとめたオブジェクト群の解説です。appオブジェクトを生成しておくと、配下クラスのインスタンス生成が不要になります。しかし、ドライバーから独立して利用するクラスの場合は、変数に代入が必要です。

オブジェクト設定

・変数宣言

Dim app As  New Selenium.Application

文法

Dim <変数名> As [メソッド,プロパティ]
Set <変数名> = App. [メソッド,プロパティ]

メソッド・プロパティ

メソッド

スクロールできます
Name解説ShortExample
ChromeDriverインスタンス生成(ChromeDriver)Set driver = App.ChromeDriver
Dictionaryインスタンス生成(Dictionary)Set Dic = App.Dictionary
FirefoxDriverインスタンス生成(FirefoxDriver)
IEDriverインスタンス生成(IEDriver)
Listインスタンス生成(List)Set List = App.List
OperaDriverインスタンス生成(OperaDriver)
PdfFileインスタンス生成(PdfFile)Set PdfFile = App.PdfFile
PhantomJSDriverインスタンス生成(PhantomJSDriver)
Pointインスタンス生成(Point)Set Point = driver.Window.Position
Sizeインスタンス生成(Size)Set Size = driver.Window.Size
Tableインスタンス生成(Table)Set Table = App.Table
WebDriverインスタンス生成(WebDriver)

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

プロパティ

スクロールできます
Name解説ShortExample
AssertAssertクラスの利用App.Assert.Equals "Google", driver.title
ByByクラスの利用driver.FindElement(App.By.Name("q")).SendKeys "hoge"
KeysKeysクラスの利用driver.Keyboard.KeyDown App.Keys.Shift
UtilsUtilsクラスの利用Debug.Print "IsMatch: " & App.Utils.IsMatch(driver.title, "Google")
VerifyVerifyクラスの利用Debug.Print App.Verify.Equals(row("ExpectedTitle"), "Wikipedia, the free encyclopedia")
WaiterWaiterクラスの利用App.Waiter.Wait 2000

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

Example

Use_Chrome

Private Sub Use_Chrome()
Dim App As New Selenium.Application
    Dim driver As ChromeDriver
    Set driver = App.ChromeDriver
    driver.Get "https://www.google.co.jp"
    driver.Keyboard.KeyDown App.Keys.Shift
    driver.FindElement(App.By.Name("q")).SendKeys "hoge"
    
    Dim Point As Selenium.Point
    Set Point = driver.Window.Position
    Debug.Print "XY: " & Point.X & " " & Point.Y
    
    Dim Size As Size
    Set Size = driver.Window.Size
    Debug.Print "SIze: " & Size.Width & " " & Size.Height
    
    App.Assert.Equals "Google", driver.title
    App.Waiter.Wait 2000
    Debug.Print "IsMatch: " & App.Utils.IsMatch(driver.title, "Google")
    
    driver.Quit
End Sub

AppCls_Table

Private Sub AppCls_Table()
    Dim App As New Selenium.Application
    Dim Table As Table, row
    Set Table = App.Table
    Dim tblSh As Worksheet: Set tblSh = ThisWorkbook.Worksheets("TableData")
    Table.From(tblSh.Range("A1")).Where ("Id > 0")
    Debug.Print "テーブルデータ(0起点): " & Table.Count
    For Each row In Table
        Debug.Print App.Verify.Equals(row("ExpectedTitle"), "Wikipedia, the free encyclopedia")
    Next row
End Sub

AppCls_DictionaryCls

Private Sub AppCls_DictionaryCls()
    Dim App As New Selenium.Application
    Dim Dic As Dictionary
    Set Dic = App.Dictionary
    Dic.Add "age", "sage"
    Debug.Print Dic.Item("age")
End Sub

AppCls_ListCls

Private Sub AppCls_ListCls()
    Dim App As New Selenium.Application
    Dim List As List
    Set List = App.List
    List.Add "hoge"
    Debug.Print List.Item(1)
End Sub

AppCls_PdfFile

Private Sub AppCls_PdfFile()
    Dim App As New Selenium.Application
    Dim PdfFile As PdfFile
    Set PdfFile = App.PdfFile
    PdfFile.AddPage
    PdfFile.AddTitle "hoge"
    PdfFile.SaveAs "C:\Users\shoji\Desktop\hoge.pdf"
End Sub
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

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