Jump to content

Instantly set textarea content based on the value of the clipboard without using sendkeys and setelementvalue


Hermes
 Share

Go to solution Solved by Danp2,

Recommended Posts

Hi, I am struggling in setting the value of a textarea based on the value of clipboard (that contains a long web page source codes). If I useĀ _WD_SetElementValue, it freezes after some time, or appears to be pressing tab and goes out of focus. I can also use send keys but i need the script to run in the background.

Here is the full script:

#Include "Chrome.au3"
#Include "wd_core.au3"
#Include "wd_helper.au3"
#Include "WinHttp.au3"
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Array.au3>
#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>
#include <GDIPlus.au3>
#include <Excel.au3>

Local $sDesiredCapabilities, $sSession
SetupChrome()

_WD_Startup()


$sSession = _WD_CreateSession($sDesiredCapabilities)

_WD_LoadWait($sSession)

_WD_Navigate($sSession, "http://demo.borland.com/testsite/stadyn_largepagewithimages.html")


_WD_LoadWait($sSession)

Global $sSource = _WD_GetSource($sSession)

Local $Paste = ClipPut($sSource)
Local $sData = ClipGet()

Local $aArray = 0, _
      $iOffset = 1

While 1
   $aArray = StringRegExp($sData, '(?s)<p>.*</p>', $STR_REGEXPARRAYMATCH, $iOffset)
   If @error Then ExitLoop
   $iOffset = @extended

   For $i = 0 To UBound($aArray) - 1
      Local $Paste = ClipPut($aArray[$i])
      Local $sRegExData = ClipGet()

      ;MsgBox(0, "", "$sRegExData = " & $sRegExData)
   Next

WEnd

_WD_Navigate($sSession, "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_textarea_placeholder")

_WD_WaitElement($sSession, $_WD_LOCATOR_ByCSSSelector, "iframe#iframeResult")
Local $sElement1 = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "iframe#iframeResult")

_WD_FrameEnter($sSession, $sElement1)

_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//html/body/textarea")
$textarea = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//html/body/textarea")
_WD_ElementAction($sSession, $textarea, 'click')
;WD SetElementValue(SsSession, Stextarea, $sRegExData) <-- I can do this but the focus goes out, or the browser freezes

_WD_FrameLeave($sSession)

sleep(2000)
Send("^v")



_WD_LoadWait($sSession)


_WD_Shutdown()

Func SetupChrome()

    _WD_Option('Driver', 'chromedriver.exe')

    _WD_Option('Port', 9515)

    _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')

    $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}'

EndFunc   ;==>SetupChrome

Can someone help me please, or re-direct me to the right path? TIA!

Edited by Hermes
Link to comment
Share on other sites

41 minutes ago, Danp2 said:

You should be able to use _WD_Action to sent a Ctrl+V and paste the clipboard contents.

I tried doing:

_WD_Action($sSession, 'actions', Send("^v"))
Ā 

It works, but if I switch to another window while the script is running, it doesn't paste it in the textarea element.

Link to comment
Share on other sites

57 minutes ago, Danp2 said:

There are examples of the proper way to do this on the forum. Here's one example --

I tried doing:

Ā 

; Press ctrl
$sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE009"}]},'

; Press v
$sAction &= '{"actions":[{"type": "key", "id": "keyboard_2", "actions": [{"type": "keyDown", "value": "v"}, {"type": "keyUp", "value": "v"}]}]}'

; Release ctrl
$sAction &= '{"type": "key", "id": "keyboard_3", "actions": [{"type": "keyUp", "value": "\uE009"}]}]}'

_WD_Action($sSession, "actions", $sAction)

Referenced from:Ā 

Ā 

But it is returning this error:

Ā 

__WD_Post: URL=HTTP://127.0.0.1:9515/session/ced70a11dda726051078d9a4aa37385e/actions; $sData={"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE009"}]},{"actions":[{"type": "key", "id": "keyboard_2", "actions": [{"type": "keyDown", "value": "v"}, {"type": "keyUp", "value": "v"}]}]}{"type": "key", "id": "keyboard_3", "actions": [{"type": "keyUp", "value": "\uE009"}]}]}
__WD_Post: StatusCode=400; ResponseText={"value":{"error":"invalid argument","message":"invalid argument: missing command parameters","stack...
__WD_Post ==> Invalid argument: {"value":{"error":"invalid argument","message":"invalid argument: missing command parameters","stacktrace":"Backtrace:\n\tOrdinal0 [0x00900C43+2493507]\n\tOrdinal0 [0x0089A4B1+2073777]\n\tOrdinal0 [0x007A2608+1058312]\n\tOrdinal0 [0x007E3ADA+1325786]\n\tOrdinal0 [0x007E351F+1324319]\n\tOrdinal0 [0x0078515F+938335]\n\tOrdinal0 [0x00785676+939638]\n\tOrdinal0 [0x007859A1+940449]\n\tGetHandleVerifier [0x00A87BF6+1548950]\n\tGetHandleVerifier [0x00B3461C+2256060]\n\tGetHandleVerifier [0x0098C13B+518107]\n\tGetHandleVerifier [0x0098B1E0+514176]\n\tOrdinal0 [0x0089F53D+2094397]\n\tOrdinal0 [0x00784EEE+937710]\n\tOrdinal0 [0x00784A02+936450]\n\tGetHandleVerifier [0x00B5A2CC+2410860]\n\tBaseThreadInitThunk [0x76306359+25]\n\tRtlGetAppContainerNamedObjectPath [0x775687A4+228]\n\tRtlGetAppContainerNamedObjectPath [0x77568774+180]\n"}}
_WD_Action: {"value":{"error":"invalid argument","message":"invalid argument: missing command parameters","stacktrace":"Backtrace:\n\tOrdinal0 [0x00900C43+2493507]\n\tOrdinal0 [0x0089A4B1+2073777]\n\tOrdinal0 [0x007A2608+1058312]\n\tOrdinal0 [0x007E3ADA+1325786]\n\tOrdinal0 [0x007E351F+1324319]\n\tOrdinal0 [0x0078515F+938335]\n\tOrdinal0 [0x00785676+939638]\n\tOrdinal0 [0x007859A1+940449]\n\tGetHandleVerifier [0x00A87BF6+1548950]\n\tGetHandleVerifier [0x00B3461C+2256060]\n\tGetHandleVerifier [0x0098C13B+518107]\n\tGetHandleVerifier [0x0098B1E0+514176]\n\tOrdinal0 [0x0089F53D+2094397]\n\tOrdinal0 [0x00784EEE+937710]\n\tOrdinal0 [0x00784A02+936450]\n\tGetHandleVerifier [0x00B5A2CC+2410860]\n\tBaseThreadInitThunk [0x76306359+25]\n\tRtlGetAppContainerNamedObjectPath [0x775687A4+228]\n\tRtlGetAppContainerNamedObjectPath [0x77568774+180]\n"}}
_WD_Action ==> Invalid argument: HTTP status = 400

Ā 

Link to comment
Share on other sites

  • Solution

The Webdriver "actions" is an advanced feature that can be difficult to format correctly. In this case, the string of actions isn't properly formatted, so the Webdriver is throwing an error. Here's a simplified example that worked for me --

; Press ctrl
$sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE009"},'

; Pause
$sAction &= '{"type": "pause", "duration": 500},'

; Press v
$sAction &= '{"type": "keyDown", "value": "v"}, {"type": "keyUp", "value": "v"},'

; Release ctrl
$sAction &= '{"type": "keyUp", "value": "\uE009"}]}]}'

_WD_Action($sSession, "actions", $sAction)

Ā 

Link to comment
Share on other sites

19 minutes ago, Danp2 said:

The Webdriver "actions" is an advanced feature that can be difficult to format correctly. In this case, the string of actions isn't properly formatted, so the Webdriver is throwing an error. Here's a simplified example that worked for me --

; Press ctrl
$sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE009"},'

; Pause
$sAction &= '{"type": "pause", "duration": 500},'

; Press v
$sAction &= '{"type": "keyDown", "value": "v"}, {"type": "keyUp", "value": "v"},'

; Release ctrl
$sAction &= '{"type": "keyUp", "value": "\uE009"}]}]}'

_WD_Action($sSession, "actions", $sAction)

Ā 

Thank you so much! It is working as expected now.

Link to comment
Share on other sites

I might add that it is aĀ safe practice to click the input element before sending "^v" keys, especially after issuing a "clear" command. Or @Danp2may know how to send the keys to the element instead of the session.

_WD_ElementAction($sSession, $sElement, "clear")
_WD_ElementAction($sSession, $sElement, "click")
Sleep(100)

; Press ctrl
$sAction = '{"actions":[{"type": "key", "id": "keyboard_1", "actions": [{"type": "keyDown", "value": "\uE009"},'

; Pause
$sAction &= '{"type": "pause", "duration": 500},'

; Press v
$sAction &= '{"type": "keyDown", "value": "v"}, {"type": "keyUp", "value": "v"},'

; Release ctrl
$sAction &= '{"type": "keyUp", "value": "\uE009"}]}]}'

_WD_Action($sSession, "actions", $sAction)

Ā 

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...