' Set value to element extended '2021/8/13 chg ishi
' ( special keys codes support )
Public Sub SetValue(ElementId As String, _
text As Variant, _
Optional ClearBeforeTyping As Boolean = True, _
Optional ByVal sessionid As String = vbNullString)
Dim data As New Dictionary
If sessionid <> vbNullString Then
data.Add "sessionId", sessionid
End If
data.Add "id", ElementId
Dim i As Integer
Dim KeyCode As String
Dim value As String
Dim chars() As String
If TypeName(text) = "Long" Then
If Keys.[_Min0] < CLng(text) And CLng(text) < Keys.[_Max0] Or _
Keys.[_Min1] < CLng(text) And CLng(text) < Keys.[_Max1] Then
KeyCode = "\ue0" & Right("0" & LCase(Hex(CLng(text))), 2)
value = KeyCode
ReDim Preserve chars(0)
chars(0) = KeyCode
Else
value = CStr(text)
ReDim chars(Len(text) - 1)
For i = 0 To UBound(chars)
chars(i) = Mid(text, i + 1, 1)
Next
End If
Else
value = CStr(text)
ReDim chars(Len(text) - 1)
For i = 0 To UBound(chars)
chars(i) = Mid(text, i + 1, 1)
Next
End If
data.Add "text", value
data.Add "value", chars
If ClearBeforeTyping = True Then Execute CMD_CLEAR_ELEMENT, data
Execute CMD_SEND_KEYS_TO_ELEMENT, data
End Sub
WebElement.cls
' Set value to element '2021/8/13 chg ishi
Public Sub SetValue(text As Variant, _
Optional ClearBeforeTyping As Boolean = True)
Driver_.SetValue ElementId_, text, ClearBeforeTyping, SessionId_
End Sub
JsonConverter.bas
Private Function json_Encode に対して、特殊キーの場合は \ をエスケープしない様にする必要がありました。
特殊キーの判定は甘いかもしれません。
Case 92
' \ -> 92 -> \\
json_Char = "\\"
' 2021/8/13 add ishi
If VBA.Len(json_Text) = 6 Then
If VBA.Mid$(json_Text, json_Index, 4) = "\ue0" Then
json_Char = "\" 'Special key codes are not escaped.
End If
End If
Case 47
Python Seleniumの send_keys メソッドを参考にして、SetValue で特殊キーを扱えるようにしてみました。 Pythonは引数の数を固定していないので Excel VBA の ParamArray でトライしましたが、WebDriver.cls と WebElement.cls での両立は私の技量ではできませんでした。そこで WebDriver.cls と WebElement.cls 共に、入力に先立ってクリアする/しない の引数(Optional ClearBeforeTyping As Boolean = True)を加えました。 また JsonConverter.bas で特殊キーの場合は \ をエスケープしない様にする必要がありました。
WebDriver.cls
WebElement.cls
JsonConverter.bas
Private Function json_Encode に対して、特殊キーの場合は \ をエスケープしない様にする必要がありました。 特殊キーの判定は甘いかもしれません。
=========================================================== 動作確認 下記の場合、このような動きになります。 ”54321” ⇒ "5432" ⇒ "542" ⇒ "42" ⇒ "42ABC" ⇒ 次のinputへタブ移動