You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TinySeleniumVBA V0.1.3 has SendKeys capability in the form of the SetValue function. @ezagdd has proposed changes to SetValue that handles special keyboard keys such as Esc, BackSpace and function keys. The command CMD_SEND_KEYS_TO_ELEMENT will process special keys, but unfortunately only in isolation (one special key per call). For example:
'send key sequence "This is COOKL!" & LEFT_key & LEFT_key & LEFT_key & DELETE_key
WebElement.SetValue "This is COOKL!"
WebElement.SetValue LEFT_key, False
WebElement.SetValue LEFT_key, False
WebElement.SetValue LEFT_key, False
WebElement.SetValue DELETE_key, False
'Prints "This is COOL!" to the immediate window
Debug.Print WebElement.GetValue
But it would be nice to be able to do something like this:
The following version of SendKeys (based on SetValue), handles the problem more elegantly...
First create a Keyboard class for special keys (which will also come in handy when we tackle Actions later):
'A class to handle special keys for SendKeys and Actions
Private mNullKey As String
Private mCancelKey As String
Private mHelpKey As String
Private mBackspaceKey As String
Private mTabKey As String
Private mClearKey As String
Private mReturnKey As String
Private mEnterKey As String
Private mShiftKey As String
Private mControlKey As String
Private mAltKey As String
Private mPauseKey As String
Private mEscapeKey As String
Private mSpaceKey As String
Private mPage_UpKey As String
Private mPage_DownKey As String
Private mEndKey As String
Private mHomeKey As String
Private mLeftKey As String
Private mUpKey As String
Private mRightKey As String
Private mDownKey As String
Private mInsertKey As String
Private mDeleteKey As String
Private mSemicolonKey As String
Private mEqualsKey As String
Private mNumpad0Key As String
Private mNumpad1Key As String
Private mNumpad2Key As String
Private mNumpad3Key As String
Private mNumpad4Key As String
Private mNumpad5Key As String
Private mNumpad6Key As String
Private mNumpad7Key As String
Private mNumpad8Key As String
Private mNumpad9Key As String
Private mMultiplyKey As String
Private mAddKey As String
Private mSeparatorKey As String
Private mSubtractKey As String
Private mDecimalKey As String
Private mDivideKey As String
Private mF1Key As String
Private mF2Key As String
Private mF3Key As String
Private mF4Key As String
Private mF5Key As String
Private mF6Key As String
Private mF7Key As String
Private mF8Key As String
Private mF9Key As String
Private mF10Key As String
Private mF11Key As String
Private mF12Key As String
Private mMetaKey As String
Private mCommandKey As String
'using Gets to make key values read-only to user
Property Get NullKey() As String
NullKey = mNullKey
End Property
Property Get CancelKey() As String
CancelKey = mCancelKey
End Property
Property Get HelpKey() As String
HelpKey = mHelpKey
End Property
Property Get BackspaceKey() As String
BackspaceKey = mBackspaceKey
End Property
Property Get TabKey() As String
TabKey = mTabKey
End Property
Property Get ClearKey() As String
ClearKey = mClearKey
End Property
Property Get ReturnKey() As String
ReturnKey = mReturnKey
End Property
Property Get EnterKey() As String
EnterKey = mEnterKey
End Property
Property Get ShiftKey() As String
ShiftKey = mShiftKey
End Property
Property Get ControlKey() As String
ControlKey = mControlKey
End Property
Property Get AltKey() As String
AltKey = mAltKey
End Property
Property Get PauseKey() As String
PauseKey = mPauseKey
End Property
Property Get EscapeKey() As String
EscapeKey = mEscapeKey
End Property
Property Get SpaceKey() As String
SpaceKey = mSpaceKey
End Property
Property Get Page_UpKey() As String
Page_UpKey = mPage_UpKey
End Property
Property Get Page_DownKey() As String
Page_DownKey = mPage_DownKey
End Property
Property Get EndKey() As String
EndKey = mEndKey
End Property
Property Get HomeKey() As String
HomeKey = mHomeKey
End Property
Property Get LeftKey() As String
LeftKey = mLeftKey
End Property
Property Get UpKey() As String
UpKey = mUpKey
End Property
Property Get RightKey() As String
RightKey = mRightKey
End Property
Property Get DownKey() As String
DownKey = mDownKey
End Property
Property Get InsertKey() As String
InsertKey = mInsertKey
End Property
Property Get DeleteKey() As String
DeleteKey = mDeleteKey
End Property
Property Get SemicolonKey() As String
SemicolonKey = mSemicolonKey
End Property
Property Get EqualsKey() As String
EqualsKey = mEqualsKey
End Property
Property Get Numpad0Key() As String
Numpad0Key = mNumpad0Key
End Property
Property Get Numpad1Key() As String
Numpad1Key = mNumpad1Key
End Property
Property Get Numpad2Key() As String
Numpad2Key = mNumpad2Key
End Property
Property Get Numpad3Key() As String
Numpad3Key = mNumpad3Key
End Property
Property Get Numpad4Key() As String
Numpad4Key = mNumpad4Key
End Property
Property Get Numpad5Key() As String
Numpad5Key = mNumpad5Key
End Property
Property Get Numpad6Key() As String
Numpad6Key = mNumpad6Key
End Property
Property Get Numpad7Key() As String
Numpad7Key = mNumpad7Key
End Property
Property Get Numpad8Key() As String
Numpad8Key = mNumpad8Key
End Property
Property Get Numpad9Key() As String
Numpad9Key = mNumpad9Key
End Property
Property Get MultiplyKey() As String
MultiplyKey = mMultiplyKey
End Property
Property Get AddKey() As String
AddKey = mAddKey
End Property
Property Get SeparatorKey() As String
SeparatorKey = mSeparatorKey
End Property
Property Get SubtractKey() As String
SubtractKey = mSubtractKey
End Property
Property Get DecimalKey() As String
DecimalKey = mDecimalKey
End Property
Property Get DivideKey() As String
DivideKey = mDivideKey
End Property
Property Get F1Key() As String
F1Key = mF1Key
End Property
Property Get F2Key() As String
F2Key = mF2Key
End Property
Property Get F3Key() As String
F3Key = mF3Key
End Property
Property Get F4Key() As String
F4Key = mF4Key
End Property
Property Get F5Key() As String
F5Key = mF5Key
End Property
Property Get F6Key() As String
F6Key = mF6Key
End Property
Property Get F7Key() As String
F7Key = mF7Key
End Property
Property Get F8Key() As String
F8Key = mF8Key
End Property
Property Get F9Key() As String
F9Key = mF9Key
End Property
Property Get F10Key() As String
F10Key = mF10Key
End Property
Property Get F11Key() As String
F11Key = mF11Key
End Property
Property Get F12Key() As String
F12Key = mF12Key
End Property
Property Get MetaKey() As String
MetaKey = mMetaKey
End Property
Property Get CommandKey() As String
CommandKey = mCommandKey
End Property
Private Sub Class_Initialize()
mNullKey = "\ue000"
mCancelKey = "\ue001"
mHelpKey = "\ue002"
mBackspaceKey = "\ue003"
mTabKey = "\ue004"
mClearKey = "\ue005"
mReturnKey = "\ue006"
mEnterKey = "\ue007"
mShiftKey = "\ue008"
mControlKey = "\ue009"
mAltKey = "\ue00a"
mPauseKey = "\ue00b"
mEscapeKey = "\ue00c"
mSpaceKey = "\ue00d"
mPage_UpKey = "\ue00e"
mPage_DownKey = "\ue00f"
mEndKey = "\ue010"
mHomeKey = "\ue011"
mLeftKey = "\ue012"
mUpKey = "\ue013"
mRightKey = "\ue014"
mDownKey = "\ue015"
mInsertKey = "\ue016"
mDeleteKey = "\ue017"
mSemicolonKey = "\ue018"
mEqualsKey = "\ue019"
mNumpad0Key = "\ue01a"
mNumpad1Key = "\ue01b"
mNumpad2Key = "\ue01c"
mNumpad3Key = "\ue01d"
mNumpad4Key = "\ue01e"
mNumpad5Key = "\ue01f"
mNumpad6Key = "\ue020"
mNumpad7Key = "\ue021"
mNumpad8Key = "\ue022"
mNumpad9Key = "\ue023"
mMultiplyKey = "\ue024"
mAddKey = "\ue025"
mSeparatorKey = "\ue026"
mSubtractKey = "\ue027"
mDecimalKey = "\ue028"
mDivideKey = "\ue029"
mF1Key = "\ue031"
mF2Key = "\ue032"
mF3Key = "\ue033"
mF4Key = "\ue034"
mF5Key = "\ue035"
mF6Key = "\ue036"
mF7Key = "\ue037"
mF8Key = "\ue038"
mF9Key = "\ue039"
mF10Key = "\ue03a"
mF11Key = "\ue03b"
mF12Key = "\ue03c"
mMetaKey = "\ue03d"
mCommandKey = "\ue03d"
End Sub
Next, Add the following to the WebDriver class:
Please Note: Below I'm using the coding styles that I discuss in The Execute Method Issue and The WebElement ID Issue discussions. But if you love dealing with WebElement ID strings instead, then it would not be difficult to convert the code below back to that style. 🙂
Public Sub SendKeys(element As WebElement, ByVal keys As String, 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", element.elementId
If ClearBeforeTyping = True Then Execute CMD_CLEAR_ELEMENT, data
'split key sequence into chunks with special keys (pattern "\ue0*") by themselves
ReDim chunk(0 To Len(keys) - 1)
i = -1
Do Until keys = ""
ipos = InStr(keys, "\ue0")
If ipos > 0 Then
If ipos > 1 Then 'text to left is chunk
i = i + 1
chunk(i) = Left(keys, ipos - 1)
End If
'now process special key
i = i + 1
chunk(i) = Mid(keys, ipos, 6)
keys = Right(keys, Len(keys) - (ipos + 5))
Else
i = i + 1
chunk(i) = keys
keys = ""
End If
Loop
ReDim Preserve chunk(0 To i)
'initialize param keys
data.Add "text", ""
data.Add "value", ""
'loop through and send text chunks to target
For i = 0 To UBound(chunk)
If Left(chunk(i), 4) = "\ue0" Then
ReDim chars(0 To 0)
chars(0) = chunk(i)
Else
ReDim chars(0 To Len(chunk(i)) - 1)
For j = 0 To Len(chunk(i)) - 1
chars(j) = Mid(chunk(i), j + 1, 1)
Next j
End If
data("text") = chunk(i)
data("value") = chars
Execute CMD_SEND_KEYS_TO_ELEMENT, data
Next i
End Sub
And this to the WebElement class:
Public Sub SendKeys(ByVal keys As String, Optional ByVal ClearBeforeTyping As Boolean = True)
Driver_.SendKeys Me, keys, ClearBeforeTyping, SessionId_
End Sub
Here is a usage case:
Sub test_sendkeys()
Dim Driver As New WebDriver
Dim keys As New KeyBoard
Driver.Chrome
Driver.OpenBrowser
Driver.Navigate "https://www.google.com/"
Driver.Wait 1000
Driver.FindElement(by.name, "q").SendKeys "This is COOKL!" & keys.LeftKey & keys.LeftKey & keys.LeftKey & keys.DeleteKey & keys.ReturnKey
Driver.Wait 2000
Driver.CloseBrowser
Driver.Shutdown
End Sub
One additional note: The following changes must be made to the json_Encode function in JSonConverter.bas as suggested by @ezagdd here:
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
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
TinySeleniumVBA V0.1.3 has SendKeys capability in the form of the SetValue function. @ezagdd has proposed changes to SetValue that handles special keyboard keys such as Esc, BackSpace and function keys. The command CMD_SEND_KEYS_TO_ELEMENT will process special keys, but unfortunately only in isolation (one special key per call). For example:
But it would be nice to be able to do something like this:
The following version of SendKeys (based on SetValue), handles the problem more elegantly...
First create a Keyboard class for special keys (which will also come in handy when we tackle Actions later):
Next, Add the following to the WebDriver class:
Please Note: Below I'm using the coding styles that I discuss in The Execute Method Issue and The WebElement ID Issue discussions. But if you love dealing with WebElement ID strings instead, then it would not be difficult to convert the code below back to that style. 🙂
And this to the WebElement class:
Here is a usage case:
One additional note: The following changes must be made to the json_Encode function in JSonConverter.bas as suggested by @ezagdd here:
Beta Was this translation helpful? Give feedback.
All reactions