Jump to content

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


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 post
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 post
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 post
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 post
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 post
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 post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Zaoka
      HI,
      for couple of years I'm using  Jos script for sending reports, email with excel attachment. But from last week i'm getting this error when sending excel or word attachment
      message has lines too long for transport jpeg, pdf works with no problems, any sugestion ?
    • By diepfeile
      I'm using the following:
      Autoit 3.3.14.5
      newly installed Beta 3.3.15.5
      SQlite version 3380000 aka 3.38.0
      I put sqlite3.dll and sqlite3_x64.dll in C:\Windows\System32 since many scripts depend on them.


      I extended the output of _SQLite_Startup()
      with:
      ConsoleWrite("@AutoItX64 " & @AutoItX64 & @CRLF) ConsoleWrite("$sDll_Filename " & $sDll_Filename & @CRLF) ConsoleWrite("_SQLite_LibVersion=" & _SQLite_LibVersion() & @CRLF)

      Also using the script from https://www.autoitscript.com/autoit3/docs/libfunctions/_SQLite_Startup.htm for testing.

       
      >Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "R:\Download\aasdf.au3" @AutoItX64 0 $sDll_Filename sqlite3.dll _SQLite_LibVersion=0 >Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "R:\Download\aasdf.au3" @AutoItX64 1 $sDll_Filename sqlite3_x64.dll _SQLite_LibVersion=3.38.0 >Running:(3.3.15.5):C:\Program Files (x86)\AutoIt3\Beta\autoit3.exe "R:\Download\aasdf.au3" @AutoItX64 0 $sDll_Filename sqlite3.dll _SQLite_LibVersion=0 >Running:(3.3.15.5):C:\Program Files (x86)\AutoIt3\Beta\autoit3_x64.exe "R:\Download\aasdf.au3" @AutoItX64 1 $sDll_Filename sqlite3_x64.dll _SQLite_LibVersion=3.38.0


      Why doesn't it work in 32bit, despite me having the 32bit sqlite.dll? Autoit urges running scripts in 32bit mode and Scite starts scripts just in 32bit mode without the flag?
      With #AutoIt3Wrapper_UseX64=Y it just works, both normal Autoit and beta!
      sqlite3.dll sqlite3_x64.dll
    • By Iraj
      Hello Team, Greetings!
      Is there any way to run any 3rd party application silently in background without the GUI getting in the front ?
      I was implementing angry-ip scanner with autoit & wanted the angry-ip application to run in background quietly as I am copying its output to other file on completion. is there any way to achieve my query?
      Below is code I tried: 
      $range = "192.168.0.1 192.168.0.255" ShellExecuteWait("C:\Windows\DDM\ipscan.exe","-f:range "&$iprange&" -q -o C:\temp\ScanResults.csv","","open",@SW_HIDE) Thanks!
    • By PeterVerbeek
      This topic give you access to an AutoIt functions library I maintain which is called PAL, Peter's AutoIt Library. The latest version 1.26 contains 214 functions divided into these topics:
      window, desktop and monitor GUI, mouse and color GUI controls including graphical buttons (jpg, png) logics and mathematics include constants string, xml string and file string dialogues and progress bars data lists: lists, stacks, shift registers and key maps (a.ka. dictionaries) miscellaneous: logging/debugging, process and system info Change log and files section  on the PAL website (SourceForge).
      A lot of these functions were created in the development of Peace, Peter's Equalizer APO Configuration Extension, which is a user interface for the system-wide audio driver called Equalizer APO.
    • By Hermes
      Hi, I am trying to select elements in print page (dialog window) for a specific web page. But when trying to do so, it looks like it does not allow me to do it.
      So far, this is what I have:
      _WD_WaitElement($sSession, $_WD_LOCATOR_ByCSSSelector, "body") Local $mainpagebody = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "body") _WD_HighlightElement($sSession, $mainpagebody, 2) _WD_WaitElement($sSession, $_WD_LOCATOR_ByCSSSelector, "img.print-button") Local $printbutton = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "img.print-button") _WD_ElementAction($sSession, $printbutton, 'click') _WD_LoadWait($sSession) Sleep(3000) _WD_WaitElement($sSession, $_WD_LOCATOR_ByCSSSelector, "body") Local $printpagebody = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "body") _WD_HighlightElement($sSession, $printpagebody, 2) The script above highlights the main web page "body" element, then clicks a tag to print a specific part of the page, then it will open a print page window where i am trying to highlight the body of that print page - but it looks like it is dropping from the session because it opens up another chrome page chrome://print.
       
      Below is the output log:
      __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/element; $sData={"using":"css selector","value":"body"} __WD_Post: StatusCode=200; ResponseText={"value":{"element-6066-11e4-a52e-4f735466cecf":"91a394f0-004c-480d-aedf-52e2b30233c6"}}... _WD_FindElement: {"value":{"element-6066-11e4-a52e-4f735466cecf":"91a394f0-004c-480d-aedf-52e2b30233c6"}} _WD_WaitElement ==> Success __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/element; $sData={"using":"css selector","value":"body"} __WD_Post: StatusCode=200; ResponseText={"value":{"element-6066-11e4-a52e-4f735466cecf":"91a394f0-004c-480d-aedf-52e2b30233c6"}}... _WD_FindElement: {"value":{"element-6066-11e4-a52e-4f735466cecf":"91a394f0-004c-480d-aedf-52e2b30233c6"}} __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/execute/sync; $sData={"script":"arguments[0].style='background: #FFFF66; border-radius: 5px; padding-left: 3px;'; return true;", "args":[{"element-6066-11e4-a52e-4f735466cecf":"91a394f0-004c-480d-aedf-52e2b30233c6"}]} __WD_Post: StatusCode=200; ResponseText={"value":true}... _WD_ExecuteScript: {"value":true}... __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/element; $sData={"using":"css selector","value":"img.print-link"} __WD_Post: StatusCode=200; ResponseText={"value":{"element-6066-11e4-a52e-4f735466cecf":"eb9e4673-4dec-4d4c-be6a-b7967743394b"}}... _WD_FindElement: {"value":{"element-6066-11e4-a52e-4f735466cecf":"eb9e4673-4dec-4d4c-be6a-b7967743394b"}} _WD_WaitElement ==> Success __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/element; $sData={"using":"css selector","value":"img.print-link"} __WD_Post: StatusCode=200; ResponseText={"value":{"element-6066-11e4-a52e-4f735466cecf":"eb9e4673-4dec-4d4c-be6a-b7967743394b"}}... _WD_FindElement: {"value":{"element-6066-11e4-a52e-4f735466cecf":"eb9e4673-4dec-4d4c-be6a-b7967743394b"}} __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/element/eb9e4673-4dec-4d4c-be6a-b7967743394b/click; $sData={"id":"eb9e4673-4dec-4d4c-be6a-b7967743394b"} __WD_Post: StatusCode=200; ResponseText={"value":null}... _WD_ElementAction: {"value":null}... __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/execute/sync; $sData={"script":"arguments[0].style='background: #FFFF66; border-radius: 5px; padding-left: 3px;'; return true;", "args":[{"element-6066-11e4-a52e-4f735466cecf":"eb9e4673-4dec-4d4c-be6a-b7967743394b"}]} __WD_Post: StatusCode=200; ResponseText={"value":true}... _WD_ExecuteScript: {"value":true}... __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/execute/sync; $sData={"script":"return document.readyState", "args":[]} __WD_Post: StatusCode=500; ResponseText={"value":{"error":"script timeout","message":"script timeout\n (Session info: chrome=92.0.4515.107)... __WD_Post ==> Webdriver Exception: {"value":{"error":"script timeout","message":"script timeout\n (Session info: chrome=92.0.4515.107)","stacktrace":"Backtrace:\n\tOrdinal0 [0x00C63733+2504499]\n\tOrdinal0 [0x00BFC401+2081793]\n\tOrdinal0 [0x00B024F0+1058032]\n\tOrdinal0 [0x00B55685+1398405]\n\tOrdinal0 [0x00B45E83+1334915]\n\tOrdinal0 [0x00B54CDB+1395931]\n\tOrdinal0 [0x00B45D4B+1334603]\n\tOrdinal0 [0x00B222B4+1188532]\n\tOrdinal0 [0x00B23149+1192265]\n\tGetHandleVerifier [0x00DDFB8C+1512252]\n\tGetHandleVerifier [0x00E8B0DF+2214031]\n\tGetHandleVerifier [0x00CE4BC3+484211]\n\tGetHandleVerifier [0x00CE3E69+480793]\n\tOrdinal0 [0x00C0218D+2105741]\n\tOrdinal0 [0x00C066E8+2123496]\n\tOrdinal0 [0x00C06827+2123815]\n\tOrdinal0 [0x00C0FB73+2161523]\n\tBaseThreadInitThunk [0x75EB62C4+36]\n\tRtlSubscribeWnfStateChangeNotification [0x77C11B69+1081]\n\tRtlSubscribeWnfStateChangeNotification [0x77C11B34+1028]\n"}} _WD_ExecuteScript: {"value":{"error":"script timeout","message":"script timeout\n (Session info: chrome=92.0.4515.107)... _WD_ExecuteScript ==> Webdriver Exception: HTTP status = 500 _WD_LoadWait ==> Webdriver Exception __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/element; $sData={"using":"css selector","value":"body"} __WD_Post: StatusCode=200; ResponseText={"value":{"element-6066-11e4-a52e-4f735466cecf":"91a394f0-004c-480d-aedf-52e2b30233c6"}}... _WD_FindElement: {"value":{"element-6066-11e4-a52e-4f735466cecf":"91a394f0-004c-480d-aedf-52e2b30233c6"}} _WD_WaitElement ==> Success __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/element; $sData={"using":"css selector","value":"body"} __WD_Post: StatusCode=200; ResponseText={"value":{"element-6066-11e4-a52e-4f735466cecf":"91a394f0-004c-480d-aedf-52e2b30233c6"}}... _WD_FindElement: {"value":{"element-6066-11e4-a52e-4f735466cecf":"91a394f0-004c-480d-aedf-52e2b30233c6"}} __WD_Post: URL=HTTP://127.0.0.1:9515/session/cd08704233a965ccbaf9292e8692c3e4/execute/sync; $sData={"script":"arguments[0].style='background: #FFFF66; border-radius: 5px; padding-left: 3px;'; return true;", "args":[{"element-6066-11e4-a52e-4f735466cecf":"91a394f0-004c-480d-aedf-52e2b30233c6"}]} __WD_Post: StatusCode=200; ResponseText={"value":true}... _WD_ExecuteScript: {"value":true}...  
      Is it even possible to select elements in print page?

×
×
  • Create New...