Control/Automate Google Chrome with AHK

Helpful script writing tricks and HowTo's
Alpha_Moose
Posts: 23
Joined: 06 May 2023, 07:15

Control/Automate Google Chrome with AHK

Post by Alpha_Moose » 03 May 2023, 14:05

Hello all,

I just wanted to share my findings/tips on controlling google chrome with AHK. This may assist you on making scripts that directly interact with google chrome without having to use coordinates / hotstrings.

1. While a google chrome window is focused (WinActivate) you can send {F12} to open Google DevTools.

2. After sending {F12} to open the 'DevTools' console you can send "{LCtrl}{LShift}P" to open the command line and send "undock into separate window" to reliably undock the devtools console into its own window (if already undocked, it simply does nothing except waste a little time (useful if included as a redundancy for the less computer literate))

2. While Google 'DevTools' is undocked and focused you can set the window as transparent (WinSetTransparent) to hide it from view and continue to have AHK pass information into it without cluttering the desired window.

3. While Google 'DevTools' are focused you can reliably open the console by again opening the command line with "{LCtrl}{LShift}P" and sending "Console" (This works 100% of the time as compared to using the default google keybind of LCtrl+` which only works if the 'whats new' tab has been closed manually)

4. Once console is open you can send anything useful to your automation directly into google chrome! Personally I inspect element > copy JS path > and add my parameters in as needed.

Code: Select all

; Open Google DevTools
		WinKill "DevTools" ; this closes any previously open dev tools windows as a redundancy
		WinActivate "Dataentry" ; This is the desired window/tab I have open on my monitor that I want to automate
		WinWaitActive "Dataentry"
		Sleep 300
		Send "{F12}" ; Open google devtools
		Sleep 1500
		Send "{LCtrl down}{LShift down}P" ; opens devtools command line
		Sleep 300
		Send "{LCtrl up}{LShift up}"
		Sleep 200
		Send "undock into separate window" ; undocks devtools from chrome tab
		Sleep 300
		Send "{Enter}"
		Sleep 300
		WinActivate "DevTools"
		WinWaitActive "DevTools"
		WinSetTransparent 0, "DevTools" ; Makes the undocked devtools window invisible as to reduce screen clutter
		Sleep 200
		Send "{Enter}"
		Sleep 200
		Send "{LCtrl down}{LShift down}P"
		Sleep 350
		Send "{LCtrl up}{LShift up}"
		Sleep 200
		Send "Console" ; Opens and focuses the devtools console so that I may begin passing in commands as needed
		Sleep 350
		Send "{Enter}"
		Sleep 200
Use case example below: After opening google dev tools with method described above, I can reliably pass data into my desired fields (in my dataentry window) using their JS Path and using vars to define what information is sent. Depending on the actions being performed I will WinActivate certain windows to perform certain tasks.

Code: Select all

				; Define devtools vars 
				jsZoneNum := "{Text}document.querySelector('#F035261412783399833').value='" . zoneNum . "'"
				jsEventType := "{Text}document.querySelector('#F049671412783315188 > div.ui-widget-content.Content.ui-corner-bottom > div:nth-child(2) > span > span').click()"
				jsZoneDesc := "{Text}document.querySelector('#F072511412783491805').value='" . zoneDesc . "'"
				jsZoneArea := "{Text}document.querySelector('#F072521412784916624').value='" . zoneArea . "'"
				jsZoneMsg := "{Text}document.querySelector('#F072651412783741073').value='" . zoneMsg . "'"
				jsSaveAdd := "{Text}document.querySelector('#zone_details_save_loop').click()"
								
				; Zone Number
				WinActivate "DevTools"
				WinWaitActive "DevTools"
				Sleep 400
				SendInput jsZoneNum
				Sleep 400
				Send "{Enter down}"
				Sleep 400
				Send "{Enter up}"
				Sleep 400
				
				; Zone Description
				SendInput jsZoneDesc
				Sleep 400
				Send "{Enter down}"
				Sleep 400
				Send "{Enter up}"
				Sleep 400
				
				If (zoneArea !== "")
				{
				; Zone Area
				SendInput jsZoneArea
				Sleep 400
				Send "{Enter down}"
				Sleep 400
				Send "{Enter up}"
				Sleep 400
				}
				
				If (zoneMsg !== "")
				{
				; Zone Message
				SendInput jsZoneMsg
				Sleep 400
				Send "{Enter down}"
				Sleep 400
				Send "{Enter up}"
				Sleep 400
				}
				
				; Event Type
				Sleep 300
				SendInput jsEventType
				Sleep 450
				Send "{Enter down}"
				Sleep 400
				Send "{Enter up}"
				Sleep 750
				WinActivate "Dataentry"
				WinWaitActive "Dataentry"
				Sleep 450
				SendInput zoneType
				Sleep 750
				Send "`t"
				Sleep 450	
				
				; Save & Add More
				WinActivate "DevTools"
				WinWaitActive "DevTools"
				Sleep 350
				Send jsSaveAdd . "`n"
				Sleep 350
				WinActivate "Dataentry"
				WinWaitActive "Dataentry"
				Sleep(1000 * zoneDelay)
If you have any questions or additional tips let me know! This is just one example of how I automate a data entry task within google chrome and saved me a bunch of time. This method was derived from this youtube tutorial https://youtu.be/2Z7X7IjHucM

User avatar
tadamm
Posts: 40
Joined: 27 Sep 2020, 11:41

Re: Control/Automate Google Chrome with AHK

Post by tadamm » 28 May 2023, 11:38

Here is a much better way to control chrome with ahk using a bit of JS


Post Reply

Return to “Tutorials (v2)”