Jump to content

WebDriver UDF - Send keyboard command (CTRL+V) through _WD_Action


Go to solution Solved by Nine,

Recommended Posts

Posted (edited)

Good evening, everyone, 

Upfront, I want to thank you for your experience, knowledge, and time. 

With a lot of help I was shown how to copy the contents of a word document:

I have manipulated the word document using values from a spreadsheet and copied the formatted word document; now I am trying to figure out how to paste the formatted clipboard into a rich-text editor field for a webmail application. 

I am selecting the correct field and I can put individual characters ";" (semi-colon) (\uE018) as an example. 

; This works for putting a semi-colon into the correct rich-text field in the webmail body
$sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE018"}, {"type": "keyUp", "value": "\uE018"}, ]}]}'

But what I can't figure out is how to send CTRL+V to paste the formatted clipboard into the rich-text field using _WD_Action (or any other method) 

_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='messageSubject']")
        $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='messageSubject']")
        _WD_ElementAction($sSession, $sElement, 'value','[FYI] ' & $aResult[$i][19] & ' Grade: ' & $aResult[$i][4] & ' - Lessons Completed ' & $aResult[$i][7] & '% - Assessments Completed '& $aResult[$i][9] & '%')

        $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//iframe[@class='cke_wysiwyg_frame cke_reset']")
        _WD_FrameEnter($sSession, $sElement)

            _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//body[@contenteditable='true']")
            $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//body[@contenteditable='true']")
            _WD_HighlightElements($sSession, $sElement, 3)
            _WD_ElementAction($sSession, $sElement, 'click')
            $sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE009"}, {"type": "keyDown", "value": "\u0056"}, {"type": "keyUp", "value": "\uE009"}, {"type": "keyUp", "value": "\u0056"}]}]}'
            ;$sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE009"}, {"type": "keyDown", "value": "\u0056"}, ]}]}'
            ;$sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyUp", "value": "\uE009"}, {"type": "keyUp", "value": "\u0056"}]}]}'
            _WD_Action($sSession, "actions", $sAction)

        _WD_FrameLeave($sSession)

What am I doing incorrectly here?

The goal is to paste the contents of the clipboard into this field. 
Also if you have a better method of getting the formatting into the field; I am all ears. I am not sold on pasting from the clipboard but that's the only method I can think of right now that keeps the formatting. When I manually paste what is on the clipboard it works correctly. I just can't figure out how to automate the pasting method with AutoIt. 

I was trying send("^V") but after researching a little bit I saw that this is a bad idea / form: 

 

Edited by ThomasBennett
Left out some thoughts and didn't want anyone to think it must be pasted; just the only method I've figured out so far.
Posted (edited)

Hi @ThomasBennett 👋 ,

the following code is not tested, but I believe this should work.

_Main()

Func _Main()
    Local Const $sValue = StringFormat( _
        '[FYI] %s Grade: %s - Lessons Completed %s % - Assessments Completed %s%', _
        $aResult[$i][19], $aResult[$i][4], $aResult[$i][7], $aResult[$i][9] _
    )

    _SetElementText("//input[@id='messageSubject']", $sValue)
    _EnterIFrame("//iframe[@class='cke_wysiwyg_frame cke_reset']")

    Local $sSelector = "//body[@contenteditable='true']"
    _HighlightElement($sSelector)
    _ClickElement($sSelector)

    Local Const $sYourRichTextFormatString = '{rtf1\ansi\line Test by \b SOLVE-SMART \b0\line}'
    ClipPut($sYourRichTextFormatString)
    _SetElementText($sSelector, ClipGet())

    _LeaveIFrame()
EndFunc

Func _FindElement($sSelector)
    Return _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sSelector)
EndFunc

Func _WaitForVisible($sSelector)
    Local Const $iTimeoutInMilliseconds = 5000
    Local Const $iElementVisibleFlag    = 1
    Local Const $iDelayInMilliseconds   = 300

    _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, $sSelector, $iDelayInMilliseconds, $iTimeoutInMilliseconds, $iElementVisibleFlag)
EndFunc

Func _SetElementText($sSelector, $sValue)
    _WaitForVisible($sSelector)
    _WD_ElementAction($sSession, _FindElement($sSelector), 'value', $sValue)
EndFunc

Func _HighlightElement($sSelector)
    Local Const $iHighlightMethod = 3
    _WD_HighlightElements($sSession, _FindElement($sSelector), $iHighlightMethod)
EndFunc

Func _ClickElement($sSelector)
    _WaitForVisible($sSelector)
    _WD_ElementAction($sSession, _FindElement($sSelector), 'click')
EndFunc

Func _EnterIFrame($sSelector)
    _WD_FrameEnter($sSession, _FindElement($sSelector))
EndFunc

Func _LeaveIFrame()
    _WD_FrameLeave($sSession)
EndFunc

As far as I understood, you already have your rich text format string in the clipboard.
If so, you can remove these two line since they are only for demo purposes.

Local Const $sYourRichTextFormatString = '{rtf1\ansi\line Test by \b SOLVE-SMART \b0\line}'
ClipPut($sYourRichTextFormatString)

Please let me know if this matches your question about how to "send" clipboard text into an element.
In case no, please provide more information like web page structure (DOM) to come up with possible better/other XPath.

Best regards
Sven

Edited by SOLVE-SMART

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

  • Solution
Posted

Based on WebDriver W3C document key \uE056 is Numpad End.  Here how to send a Ctrl-V.

Local $sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE009"}, ' & _
  '{"type": "keyDown", "value": "v"}, {"type": "keyUp", "value": "v"}, {"type": "keyUp", "value": "\uE009"}]}]}'
_WD_Action($sSession, "actions", $sAction)

Tested and working...

Posted
On 2/8/2025 at 1:43 AM, SOLVE-SMART said:

Hi @ThomasBennett 👋 ,

the following code is not tested, but I believe this should work.

_Main()

Func _Main()
    Local Const $sValue = StringFormat( _
        '[FYI] %s Grade: %s - Lessons Completed %s % - Assessments Completed %s%', _
        $aResult[$i][19], $aResult[$i][4], $aResult[$i][7], $aResult[$i][9] _
    )

    _SetElementText("//input[@id='messageSubject']", $sValue)
    _EnterIFrame("//iframe[@class='cke_wysiwyg_frame cke_reset']")

    Local $sSelector = "//body[@contenteditable='true']"
    _HighlightElement($sSelector)
    _ClickElement($sSelector)

    Local Const $sYourRichTextFormatString = '{rtf1\ansi\line Test by \b SOLVE-SMART \b0\line}'
    ClipPut($sYourRichTextFormatString)
    _SetElementText($sSelector, ClipGet())

    _LeaveIFrame()
EndFunc

Func _FindElement($sSelector)
    Return _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sSelector)
EndFunc

Func _WaitForVisible($sSelector)
    Local Const $iTimeoutInMilliseconds = 5000
    Local Const $iElementVisibleFlag    = 1
    Local Const $iDelayInMilliseconds   = 300

    _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, $sSelector, $iDelayInMilliseconds, $iTimeoutInMilliseconds, $iElementVisibleFlag)
EndFunc

Func _SetElementText($sSelector, $sValue)
    _WaitForVisible($sSelector)
    _WD_ElementAction($sSession, _FindElement($sSelector), 'value', $sValue)
EndFunc

Func _HighlightElement($sSelector)
    Local Const $iHighlightMethod = 3
    _WD_HighlightElements($sSession, _FindElement($sSelector), $iHighlightMethod)
EndFunc

Func _ClickElement($sSelector)
    _WaitForVisible($sSelector)
    _WD_ElementAction($sSession, _FindElement($sSelector), 'click')
EndFunc

Func _EnterIFrame($sSelector)
    _WD_FrameEnter($sSession, _FindElement($sSelector))
EndFunc

Func _LeaveIFrame()
    _WD_FrameLeave($sSession)
EndFunc

As far as I understood, you already have your rich text format string in the clipboard.
If so, you can remove these two line since they are only for demo purposes.

Local Const $sYourRichTextFormatString = '{rtf1\ansi\line Test by \b SOLVE-SMART \b0\line}'
ClipPut($sYourRichTextFormatString)

Please let me know if this matches your question about how to "send" clipboard text into an element.
In case no, please provide more information like web page structure (DOM) to come up with possible better/other XPath.

Best regards
Sven

Thank you for this information @SOLVE-SMART, sadly I wasn't able to get it figured out and working correctly on my end. 

Posted
1 hour ago, Nine said:

Based on WebDriver W3C document key \uE056 is Numpad End.  Here how to send a Ctrl-V.

Local $sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE009"}, ' & _
  '{"type": "keyDown", "value": "v"}, {"type": "keyUp", "value": "v"}, {"type": "keyUp", "value": "\uE009"}]}]}'
_WD_Action($sSession, "actions", $sAction)

Tested and working...

@Nine, thank you! I was looking at the wrong keyboard list! This is now working as intended! 

Posted
17 hours ago, ThomasBennett said:

Thank you for this information @SOLVE-SMART, sadly I wasn't able to get it figured out and working correctly on my end. 

Okay. It would be interesting what was not working exactly, but I see @Nines approach was helpful enough, so all good.
Nines answer was exactly what you was looking for "sending" ctrl+v by the WebDriver protocol ... I understood this now 😅 .
My approach was looking for simply pasting the formated string to the element which is probably not feasable (that's why I wrote "not tested") in this way. I will test it - just to learn about it.

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...