uezo / TinySeleniumVBA

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

Add Method. CurrentURL #13

Open ghost opened 3 years ago

ghost commented 3 years ago

TinySeleniumVBA WebDriver.cls に対して、CurrentURL のメソッド追加。

' ==========================================================================
' Browser operations
' ==========================================================================

' Current URL                   '2021/8/7 add ishi
Public Function CurrentURL(Optional ByVal sessionId As String = vbNullString) As String
    Dim data As New Dictionary
    If sessionId <> vbNullString Then
        data.Add "sessionId", sessionId
    End If

    With CreateObject("ScriptControl")
        .Language = "JScript"
        CurrentURL = .CodeObject.decodeURI(Execute(CMD_GET_CURRENT_URL, data))
    End With
End Function
GCuser99 commented 2 years ago

I get a Class Not Registered on my 64bit Windows when I try to run CurrentURL. However it works when I replace the "With" structure with:

CurrentURL = Execute(CMD_GET_CURRENT_URL, data)

or

CurrentURL = Replace(Execute(CMD_GET_CURRENT_URL, Data), "data:,", "")
GCuser99 commented 2 years ago

Maybe this issue should be classified as a "bug", as it may require a 64-bit user to install a 32-bit version of MS Script Control dll for it to work? Google "Class Not Registered script control vba" for more info. I'll submit this as a separate issue https://github.com/uezo/TinySeleniumVBA/issues/44...

ghost commented 2 years ago

ありがとうございます。 URLに日本語がある場合はデコードする必要があります。そこで javascript の decodeURL を使うように変更しました。

Public Function CurrentURL(Optional ByVal SessionId As String = vbNullString) As String
    Dim Data As New Dictionary
    If SessionId <> vbNullString Then
        Data.Add "sessionId", SessionId
    End If

    Dim encodeURL As String
    encodeURL = Execute(CMD_GET_CURRENT_URL, Data)
    CurrentURL = Me.ExecuteScript("return decodeURI('" & encodeURL & "')")
End Function

  確認用です。

Sub TestforCurrentURL()
Dim Driver As WebDriver

    Set Driver = New WebDriver
    Driver.Chrome "chromedriver.exe"
    Driver.OpenBrowser

    Driver.Navigate "E:\Work2\テスト.html"
    MsgBox Driver.CurrentURL

    Driver.CloseBrowser
    Driver.Shutdown
    Set Driver = Nothing
End Sub

この場合の結果は以下になります。  encodeURL ⇒ file:///E:/Work2/%E3%83%86%E3%82%B9%E3%83%88.html  CurrentURL ⇒ file:///E:/Work2/テスト.html

GCuser99 commented 2 years ago

@ezagdd excellent, thanks!