Pasting text to a specific Textfield Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Loop
Posts: 67
Joined: 07 Jan 2019, 14:51

Pasting text to a specific Textfield

05 Mar 2019, 14:37

Hi,
How can I send values to a text field, with Chrome.ahk
for Example gmail:


Code: Select all

mail := "[email protected]"
PageInstance.Evaluate("document.querySelector('#identifierId')").value="%mail%"
For help would be appreciated
gregster
Posts: 5550
Joined: 30 Sep 2013, 06:48

Re: Pasting text to a specific Textfield  Topic is solved

05 Mar 2019, 15:09

Search box on https://www.autohotkey.com/boards/:

Code: Select all

var := "Test"
PageInst.Evaluate("document.querySelector('input[name=keywords]').value = '" var "' ")	


Gmail:

Code: Select all

var := "[email protected]"
PageInst.Evaluate("document.querySelector('input[name=identifier]').value = '" var "' ")	
Loop
Posts: 67
Joined: 07 Jan 2019, 14:51

Re: Pasting text to a specific Textfield

05 Mar 2019, 17:25

Works great :)
Do you have any idea, ow do I auto-click a button?

Code: Select all

 PageInstance.Evaluate("document.getElementsByClassName(" RveJvd snByac ")[0].click")
gregster
Posts: 5550
Joined: 30 Sep 2013, 06:48

Re: Pasting text to a specific Textfield

05 Mar 2019, 23:53

This works for me on this Gmail button:

Code: Select all

PageInstance.Evaluate("document.getElementsByClassName('RveJvd snByac')[0].click()")
Important are the quoting style for literal strings inside the javascript part ( ' instead of " ) and the () behind the click() method.
With your syntax use, the literal string RveJvd snByac ended up being interpreted as variable names (expression syntax) - in contrast, var in the examples above was really used as a variable.

This is a javascript way of doing it - with Chrome.ahk you could also navigate through the node structure, determine coordinates for the button and use the Input.dispatchMouseEvent method instead. That is an alternative, if the click() method doesn't work for some reason (seems to happen on some websites).
magusneo
Posts: 37
Joined: 30 Sep 2013, 06:34

Re: Pasting text to a specific Textfield

16 Mar 2019, 22:12

gregster wrote:
05 Mar 2019, 23:53
This is a javascript way of doing it - with Chrome.ahk you could also navigate through the node structure, determine coordinates for the button and use the Input.dispatchMouseEvent method instead. That is an alternative, if the click() method doesn't work for some reason (seems to happen on some websites).

Hi,gregster.
Is there any example for Input.dispatchMouseEvent usage?If the click doesn't work.
gregster
Posts: 5550
Joined: 30 Sep 2013, 06:48

Re: Pasting text to a specific Textfield

17 Mar 2019, 02:43

magusneo wrote:
16 Mar 2019, 22:12
Is there any example for Input.dispatchMouseEvent usage?If the click doesn't work.
Not yet on the forum, I think. It's use with AHK is still relatively new compared to IE COM. But here is some example usage pulled from my experiments with Chrome.ahk (I plan to post some helper functions for the library in the near future). Btw, there are more options available via https://chromedevtools.github.io/devtools-protocol/tot/Input

Perhaps it can be further optimized, but this works for me most of the time (you could, for example, check if the button press was succesful and if not, send the click again):

Code: Select all

;[...] Initialize your Chrome instance first
 
; Navigate to the desired URL 
	url := "https://www.autohotkey.com/"	
	PageInst.Call("Page.navigate", {"url": url})
	PageInst.WaitForLoad()
	sleep 500
; Determine x, y, w(idth), h(eight) of button to click
	attribute := "mbr-buttons__btn btn btn-lg animated fadeInUp delay btn-success"	; class of the desired button
	index := 1
	objPos := Chrm_GetPosition(PageInst, "Class", attribute, index)
; Determine center of button and send click to Chrome browser
	Chrm_ClickCenter(PageInst, objPos ,  "left", 1,  100)
;[...] Close your Chrome instance
return

Esc::ExitApp 

; helper functions ------------------------------------------------
Chrm_ClickCenter(PageInst, Pos ,button := "left", count :=1, delay := 100){
   x := Pos.x + Pos.w/2,     y := Pos.y + Pos.h/2
   PageInst.Call("Input.dispatchMouseEvent" , {"type" : "mousePressed", "x" : x , "y" : y, "button" : button, "clickCount" : count } ) 
   sleep, delay 
   PageInst.Call("Input.dispatchMouseEvent" , {"type" : "mouseReleased", "x" : x , "y" : y, "button" : button, "clickCount" : count } ) 
   return
}

Chrm_GetPosition(PageInst, method :="Class" , attribute :="" ,Index := 1){
  obj:={}
  oMethods := {"class" : "sByClassName", "name" : "sByName" , "tag" : "sByTagName", "id" : "ById"}
  if oMethods.HasKey(method)
       element := oMethods[method]
  else                                                                                    
  {
       msgbox "Error in Chrm_GetPosition: Unknown method"
       return "unkown method"
 }
  index := (method = "id") ? "" : "[" index-1 "]"
 
  obj.x := PageInst.Evaluate("document.getElement" element "('" Attribute "')" index ".getBoundingClientRect().x").value
  obj.y := PageInst.Evaluate("document.getElement" element "('" Attribute "')" index ".getBoundingClientRect().y").value
  obj.w := PageInst.Evaluate("document.getElement" element "('" Attribute "')" index ".getBoundingClientRect().width").value
  obj.h := PageInst.Evaluate("document.getElement" element "('" Attribute "')" index ".getBoundingClientRect().height").value
  return obj
}
This should click the 'Download' button on the AutoHotkey startpage and take you to the 'Downloads' page https://www.autohotkey.com/download/
I assume click() would work here, too, but I haven't tested it. This is only for demonstration purposes - obviously you could also directly navigate to the page, in this case. Hope this helps!
magusneo
Posts: 37
Joined: 30 Sep 2013, 06:34

Re: Pasting text to a specific Textfield

17 Mar 2019, 07:41

gregster wrote:
17 Mar 2019, 02:43
This should click the 'Download' button on the AutoHotkey startpage and take you to the 'Downloads' page https://www.autohotkey.com/download/
I assume click() would work here, too, but I haven't tested it. This is only for demonstration purposes - obviously you could also directly navigate to the page, in this case. Hope this helps!
Thank you. :thumbup:

Return to “Ask For Help”

Who is online

Users browsing this forum: CotswoldsMaker, Victor_da and 50 guests