uezo / TinySeleniumVBA

A tiny Selenium wrapper written in pure VBA
MIT License
60 stars 17 forks source link

Modify Method. Edge, Chrome, IE, Start #16

Open ghost opened 3 years ago

ghost commented 3 years ago

WebDriver.cls msedgedriver.exe, chromedriver.exe がパスが通ったフォルダにある場合は、第一引数のdriverpathを省略してもよいと思います。 また、Shell関数で起動できなかった場合のメッセージに driverpath値 を加えた方がよいと思います。

' Launch Edge Driver            '2021/8/7 chg ishi
Public Sub Edge(Optional ByVal driverPath As String = "msedgedriver.exe", _
                Optional ByVal driverUrl As String = "http://localhost:9515", _
                Optional ByVal VbAppWinStyle As Long = vbMinimizedNoFocus)
    Start driverPath, driverUrl, VbAppWinStyle
End Sub

' Launch Chrome Driver          '2021/8/7 chg ishi
Public Sub Chrome(Optional ByVal driverPath As String = "chromedriver.exe", _
                  Optional ByVal driverUrl As String = "http://localhost:9515", _
                  Optional ByVal VbAppWinStyle As Long = vbMinimizedNoFocus)        '2021/6/5 ishi Port 9151 -> 9515
    Start driverPath, driverUrl, VbAppWinStyle
End Sub

' Start WebDriver               '2021/8/7 chg ishi
Public Sub Start(ByVal driverPath As String, _
                 ByVal driverUrl As String, _
                 ByVal VbAppWinStyle As Long)

    ' Start WebDriver executable
    If Shell(driverPath, VbAppWinStyle) = 0 Then
        MsgBox "Failed in starting WebDriver." & vbCrLf & _
               "WebDriverPath : " & driverPath, vbCritical + vbOKOnly
        End
    End If

    ' Set WebDriver url
    UrlBase = driverUrl

    ' Initialize driver commands
    InitCommands
End Sub
ghost commented 3 years ago

WebDriver.cls Edge, Chrom の第三引数用に、Enumを定義しました。

Public Enum AppWinStyle         '2021/8/10 add ishi
    vbHide = 0
    'vbNormalFocus = 1
    'vbMinimizedFocus = 2
    'vbMaximizedFocus = 3
    'vbNormalNoFocus = 4
    vbMinimizedNoFocus = 6
End Enum

Edge, Chrom の第三引数の属性を上述の Enum で定義した AppWinStyle に変更しました。 Start は変更はありません。

' Launch Edge Driver            '2021/8/10 chg ishi
Public Sub Edge(Optional ByVal driverPath As String = "msedgedriver.exe", _
                Optional ByVal driverUrl As String = "http://localhost:9515", _
                Optional ByVal VbAppWinStyle As AppWinStyle = vbMinimizedNoFocus)
    Start driverPath, driverUrl, VbAppWinStyle
End Sub

' Launch Chrome Driver          '2021/8/10 chg ishi
Public Sub Chrome(Optional ByVal driverPath As String = "chromedriver.exe", _
                  Optional ByVal driverUrl As String = "http://localhost:9515", _
                  Optional ByVal VbAppWinStyle As AppWinStyle = vbMinimizedNoFocus)     '2021/6/5 ishi Port 9151 -> 9515
    Start driverPath, driverUrl, VbAppWinStyle
End Sub

' Start WebDriver               '2021/8/7 chg ishi
Public Sub Start(ByVal driverPath As String, _
                 ByVal driverUrl As String, _
                 ByVal VbAppWinStyle As Long)

    ' Start WebDriver executable
    If Shell(driverPath, VbAppWinStyle) = 0 Then
        MsgBox "Failed in starting WebDriver." & vbCrLf & _
               "WebDriverPath : " & driverPath, vbCritical + vbOKOnly
        End
    End If

    ' Set WebDriver url
    UrlBase = driverUrl

    ' Initialize driver commands
    InitCommands
End Sub
ghost commented 3 years ago

WebDriver.cls IEDriverserver対応に伴う箇所は、2021/10/16のコメント箇所になります。 上述の2つの commented記事 を包含してあります。 msedgedriver.exe, chromedriver.exe, IEDriverServer.exe がパスが通ったフォルダにある場合は、 第一引数のdriverpathを省略してもよい事にしてあります。

Public Enum AppWinStyle         '2021/8/10 add ishi
    vbHide = 0
    'vbNormalFocus = 1
    'vbMinimizedFocus = 2
    'vbMaximizedFocus = 3
    'vbNormalNoFocus = 4
    vbMinimizedNoFocus = 6
End Enum
' ==========================================================================
' Setup and shutdown
' ==========================================================================

' Launch Edge Driver            '2021/8/10 chg ishi
Public Sub Edge(Optional ByVal driverPath As String = "msedgedriver.exe", _
                Optional ByVal driverUrl As String = "http://localhost:9515", _
                Optional ByVal VbAppWinStyle As AppWinStyle = vbMinimizedNoFocus)
    Start driverPath, driverUrl, VbAppWinStyle
    DriverName = "msedgedriver"         '2021/9/20 ishi add
End Sub

' Launch Chrome Driver          '2021/8/10 chg ishi
Public Sub Chrome(Optional ByVal driverPath As String = "chromedriver.exe", _
                  Optional ByVal driverUrl As String = "http://localhost:9515", _
                  Optional ByVal VbAppWinStyle As AppWinStyle = vbMinimizedNoFocus)     '2021/6/5 ishi Port 9151 -> 9515
    Start driverPath, driverUrl, VbAppWinStyle
    DriverName = "chromedriver"         '2021/9/20 ishi add
End Sub

' Launch IE Driver              '2021/10/16 add ishi
Public Sub IE(Optional ByVal driverPath As String = "IEDriverServer.exe", _
              Optional ByVal driverUrl As String = "http://localhost:5555", _
              Optional ByVal VbAppWinStyle As AppWinStyle = vbMinimizedNoFocus)
    Start driverPath, driverUrl, VbAppWinStyle
    DriverName = "IEDriverServer"
End Sub

' Start WebDriver               '2021/8/7 chg ishi -> 2021/10/16 chg ishi
Public Sub Start(ByVal driverPath As String, _
                 ByVal driverUrl As String, _
                 ByVal VbAppWinStyle As Long)

    ' Port                          '2021/10/16 add ishi
    Dim Port As String
    Port = vbNullString
    If InStr(driverPath, "msedgedriver") > 0 Then
        Port = " --port=" & Mid(driverUrl, InStrRev(driverUrl, ":") + 1)
    ElseIf InStr(driverPath, "chromedriver") > 0 Then
        Port = " --port=" & Mid(driverUrl, InStrRev(driverUrl, ":") + 1)
    ElseIf InStr(driverPath, "IEDriverServer") > 0 Then
        Port = " /port=" & Mid(driverUrl, InStrRev(driverUrl, ":") + 1)
    End If

    ' Start WebDriver executable    '2021/10/16 chg ishi
    If Shell(driverPath & Port, VbAppWinStyle) = 0 Then
        MsgBox "Failed in starting WebDriver." & vbCrLf & _
               "WebDriverPath : " & driverPath & Port, vbCritical + vbOKOnly
        End
    End If

    ' Set WebDriver url
    UrlBase = driverUrl

    ' Initialize driver commands
    InitCommands
End Sub

' Shutdown WebDriver
Public Sub Shutdown()
    Execute CMD_SHUTDOWN
End Sub

JsonConverter.bas IEDriverserver対応に伴う箇所は、2021/10/16のコメント箇所になります。

Public Function ParseJson(ByVal JsonString As String) As Object
    Dim json_Index As Long
    json_Index = 1

    ' Remove vbCr, vbLf, and vbTab from json_String
    JsonString = VBA.Replace(VBA.Replace(VBA.Replace(JsonString, VBA.vbCr, ""), VBA.vbLf, ""), VBA.vbTab, "")

    json_SkipSpaces JsonString, json_Index
    Select Case VBA.Mid$(JsonString, json_Index, 1)
    Case "{"
        Set ParseJson = json_ParseObject(JsonString, json_Index)
    Case "["
        Set ParseJson = json_ParseArray(JsonString, json_Index)
    '2021/10/16 add start ishi  Support for IEDriverserver
    Case "<"
        Set ParseJson = New Dictionary
    '2021/10/16 add ended ishi
    Case Else
        ' Error: Invalid JSON string
        Err.Raise 10001, "JSONConverter", json_ParseErrorMessage(JsonString, json_Index, "Expecting '{' or '['")
    End Select
End Function
GCuser99 commented 2 years ago

Another possibility for handling the optional driverPath in Start, Chrome, and Edge methods:

Public Sub Edge(Optional ByVal driverPath As String="msedgedriver.exe", Optional ByVal driverUrl As String = "http://localhost:9515", Optional ByVal VbAppWinStyle As AppWinStyle = vbMinimizedNoFocus)

        driverPath = GetAbsolutePath(driverPath) 'support for relative path such as driverPath=".\msedgedriver.exe"
....

Here is the utility function for handling a relative path (if caller provides an absolute path, then function returns same):

Private Function GetAbsolutePath(ByVal strPath As String) As String
    Dim fso As New FileSystemObject
    saveppath = CurDir()
    ChDrive ThisWorkbook.path
    ChDir ThisWorkbook.path
    GetAbsolutePath = fso.GetAbsolutePathName(strPath)
    ChDrive saveppath
    ChDir saveppath
End Function