Chrome edit box send text to ID field(edit field) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Chrome edit box send text to ID field(edit field)

03 Sep 2019, 12:22

Hi Community :wave:

How can I send some text to a page with identified id

this is html element

Code: Select all

<input name="keywords" id="keywords" type="search" maxlength="128" title="Search for keywords" class="inputbox search tiny" size="20" value="" placeholder="Search…">
trying to automate some stuff.
should work on already opened chrome.

don't want:
send tab these many times and SendInput

Code: Select all

F2::
    Send ^+j
    Sleep 3000
    SendRaw document.getElementById("header-search-bar").value="Hello world 1"
    Send {Enter}
	Sleep 100
    Send ^+j
return
the above is good but don't want to go with ^+J and show user
Just like to have it magically appear there.

thank you in advance for your time and support :)
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Chrome edit box send text to ID field(edit field)

03 Sep 2019, 13:42

Try this:

Code: Select all

F2::
    Send {F6}
    SendRaw javascript:void(document.getElementById("header-search-bar").value="Hello world 1")
    Send {Enter}
return
gregster
Posts: 9113
Joined: 30 Sep 2013, 06:48

Re: Chrome edit box send text to ID field(edit field)

03 Sep 2019, 14:21

You could use the Chrome.ahk library (or Selenium), but "already opened" only works, if the Chrome browser was opened in "debug mode"...

Probably Acc.ahk as an alterntive.
ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Re: Chrome edit box send text to ID field(edit field)

03 Sep 2019, 14:23

thank you Getfree, but can we have something more robust I mean to say, If I need to update 10 edit boxes, it will be a lot of time to literally sendraw.
ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Re: Chrome edit box send text to ID field(edit field)

03 Sep 2019, 14:24

gregster wrote:
03 Sep 2019, 14:21
You could use the Chrome.ahk library (or Selenium), but "already opened" only works, if the Chrome browser was opened in 'debug mode'...
I already looked into it but need to close any existing tabs and it will start chrome again in debug mode I guess.
closing existing chrome tabs wont suit the requirement.
gregster
Posts: 9113
Joined: 30 Sep 2013, 06:48

Re: Chrome edit box send text to ID field(edit field)

03 Sep 2019, 14:26

I'd start Chrome right from the start in debug mode - no restart required. ;) It's not really obvious to the user.

Perhaps Acc.ahk instead...
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Chrome edit box send text to ID field(edit field)

03 Sep 2019, 15:25

ahklearner wrote:
03 Sep 2019, 14:23
thank you Getfree, but can we have something more robust I mean to say, If I need to update 10 edit boxes, it will be a lot of time to literally sendraw.
Not if you use the clipboard. Pasting text is instantaneous, whereas SendInput is slow as hell.
ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Re: Chrome edit box send text to ID field(edit field)

03 Sep 2019, 15:33

Getfree wrote:
03 Sep 2019, 15:25
ahklearner wrote:
03 Sep 2019, 14:23
thank you Getfree, but can we have something more robust I mean to say, If I need to update 10 edit boxes, it will be a lot of time to literally sendraw.
Not if you use the clipboard. Pasting text is instantaneous, whereas SendInput is slow as hell.
I tried pasting the javascript but it is taking it as url and google page is coming.

can we like inject JS without user seeing it in navigation bar from backend ;)

I was thinking of something dynamic and magical like AutoHotkey :)
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Chrome edit box send text to ID field(edit field)  Topic is solved

03 Sep 2019, 18:51

I think SendInput is pretty fast.

Code: Select all

F2::
    Send {F6}
    SendInput {Text}javascript:void(document.getElementById("header-search-bar").value="Hello world 1")
    Send {Enter}
return

The following code uses Acc.ahk, it works even when Chrome window is inactive.

Code: Select all

js := "void(document.getElementById('header-search-bar').value='Hello world 1')"
ChromeRunJs(js)

ChromeRunJs(ByRef js) {
	static addrBar
	if !addrBar || !WinExist("ahk_id " addrBar.hWnd) {
		if !addrBar := FindAddressBar() {
			throw "FindAddressBar failed."
		}
	}

	addrBar.accObj.accValue(0) := "javascript" . Chr(58) . js
	addrBar.accObj.accSelect(0x1, 0)
	ControlSend,, {Enter}
}

FindAddressBar(WinTitle := "ahk_exe chrome.exe", oAcc := "", accPath := "") {
	if !oAcc {
		if hWnd := WinExist(WinTitle) {
			oAcc := Acc_ObjectFromWindow(hWnd)
			while oAcc.accRole(0) != 9 { ; ROLE_SYSTEM_WINDOW := 9
				oAcc := Acc_Parent(oAcc)
			}
		} else {
			throw WinTitle " not found."
		}
		
	}

	for i, child in Acc_Children(oAcc) {
		nRole := child.accRole(0)
		
		if (nRole = 42) { ; ROLE_SYSTEM_TEXT := 42
			if (child.accName(0) ~= "i)address|地址") {
				accPath := LTrim(accPath "." i, ".")
				return {accObj: child, accPath: accPath, hWnd: hWnd}
			}
		}

		/*
			ROLE_SYSTEM_APPLICATION := 14
			ROLE_SYSTEM_PANE        := 16
			ROLE_SYSTEM_GROUPING    := 20
			ROLE_SYSTEM_TOOLBAR     := 22
			ROLE_SYSTEM_COMBOBOX    := 46
		*/
		static oGroup := {14:1, 16:1, 20:1, 22:1, 46:1}
		if oGroup.HasKey(nRole) {
			if result := FindAddressBar(, child, accPath "." i) {
				return result
			}
		}
	}
}
The forum does not allow the string:
javascript.png
javascript.png (862 Bytes) Viewed 4605 times
to appear in the posts, so I used "javascript" . Chr(58) instead.
ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Re: Chrome edit box send text to ID field(edit field)

04 Sep 2019, 07:54

tmplinshi wrote:
03 Sep 2019, 18:51
I think SendInput is pretty fast.

Code: Select all

F2::
    Send {F6}
    SendInput {Text}javascript:void(document.getElementById("header-search-bar").value="Hello world 1")
    Send {Enter}
return

The following code uses Acc.ahk, it works even when Chrome window is inactive.

Code: Select all

js := "void(document.getElementById('header-search-bar').value='Hello world 1')"
ChromeRunJs(js)

ChromeRunJs(ByRef js) {
	static addrBar
	if !addrBar || !WinExist("ahk_id " addrBar.hWnd) {
		if !addrBar := FindAddressBar() {
			throw "FindAddressBar failed."
		}
	}

	addrBar.accObj.accValue(0) := "javascript" . Chr(58) . js
	addrBar.accObj.accSelect(0x1, 0)
	ControlSend,, {Enter}
}

FindAddressBar(WinTitle := "ahk_exe chrome.exe", oAcc := "", accPath := "") {
	if !oAcc {
		if hWnd := WinExist(WinTitle) {
			oAcc := Acc_ObjectFromWindow(hWnd)
			while oAcc.accRole(0) != 9 { ; ROLE_SYSTEM_WINDOW := 9
				oAcc := Acc_Parent(oAcc)
			}
		} else {
			throw WinTitle " not found."
		}
		
	}

	for i, child in Acc_Children(oAcc) {
		nRole := child.accRole(0)
		
		if (nRole = 42) { ; ROLE_SYSTEM_TEXT := 42
			if (child.accName(0) ~= "i)address|地址") {
				accPath := LTrim(accPath "." i, ".")
				return {accObj: child, accPath: accPath, hWnd: hWnd}
			}
		}

		/*
			ROLE_SYSTEM_APPLICATION := 14
			ROLE_SYSTEM_PANE        := 16
			ROLE_SYSTEM_GROUPING    := 20
			ROLE_SYSTEM_TOOLBAR     := 22
			ROLE_SYSTEM_COMBOBOX    := 46
		*/
		static oGroup := {14:1, 16:1, 20:1, 22:1, 46:1}
		if oGroup.HasKey(nRole) {
			if result := FindAddressBar(, child, accPath "." i) {
				return result
			}
		}
	}
}
The forum does not allow the string: javascript.png
to appear in the posts, so I used "javascript" . Chr(58) instead.
Thank you @Getfree, @gregster, @tmplinshi for all your help and support, you made my day :)
ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Re: Chrome edit box send text to ID field(edit field)

06 Sep 2019, 08:55

Guys, another help please...
How to click on this "Next" button
Thank you in advance.

<div class="Button---btn_wrapper Button---default_direction appian-context-first-in-list Button---pointer_events">
<button type="button" class="Button---btn Button---default_direction Button---primary appian-context-first-in-list">Next</button></div>
ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Re: Chrome edit box send text to ID field(edit field)

06 Sep 2019, 09:15

another request..
the id "479511c552626f98c6c2fa346efa7aef" is always changing on this page, what else can I use to automatically add text in edit box, Thank you

<div class="ColumnLayout---column ColumnLayout---align_start ColumnLayout---top" data-padding-left="0" data-padding-right="-20"><div class="BoxLayout---box BoxLayout---info BoxLayout---margin_below_none"><div class="BoxLayout---box_heading_wrapper"><div class="BoxLayout---box_heading" role="heading" aria-level="2" title="Key Development ID">Key Development ID</div></div><div aria-hidden="false" class="rah-static rah-static--height-auto" style="height: auto !important; overflow: visible !important;"><div><div class="BoxLayout---box_body"><div class="ContentLayout---content_layout ContentLayout---padding_less"><div class="FieldLayout---field_layout appian-context-last-in-list"><div class="FieldLayout---accessibilityhidden"><label class="FieldLayout---field_label" for="479511c552626f98c6c2fa346efa7aef"></label></div><div class="FieldLayout---input_below"><div><input id="479511c552626f98c6c2fa346efa7aef" type="text" class="TextInput---text TextInput---align_start" aria-labelledby="479511c552626f98c6c2fa346efa7aef" placeholder="" value=""></div></div></div></div></div></div></div></div></div>
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Chrome edit box send text to ID field(edit field)

06 Sep 2019, 09:39

Open Chrome DevTools to copy the element's Selector, and then for exampe:

Code: Select all

ChromeRunJs("document.querySelector('#search > fieldset > a').click()")
I think you should post your new questions in a new topic.
ahklearner
Posts: 313
Joined: 23 Jan 2015, 01:49

Re: Chrome edit box send text to ID field(edit field)

09 Sep 2019, 07:26

tmplinshi wrote:
06 Sep 2019, 09:39
Open Chrome DevTools to copy the element's Selector, and then for exampe:

Code: Select all

ChromeRunJs("document.querySelector('#search > fieldset > a').click()")
I think you should post your new questions in a new topic.
Sure, Thank you :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], peter_ahk and 364 guests