Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Helpful script writing tricks and HowTo's

Which way have you used AHK and Chrome?

Chrome.AHK
11
33%
Auto Control Mangaer
3
9%
Selenium
9
27%
Chrome Console
3
9%
Just send commands
2
6%
Other
5
15%
 
Total votes: 33

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

Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Post by tadamm » 28 Jan 2021, 15:18

With the "death" of IE I have seen more then ever the need to move to chrome while not losing our love for AHK. I have made a few videos I hope will help others with moving to chrome. I know there are some post here talking about some of these but didn't seem to find a one stop spot with multiple ways ENJOY! :D

Selenium tends to be a popular way and by far my favorite way in AHK Chrome. Sadly if you work at a large company with a lot of IT security stuff as I do this may not work for you. Need Admin access. This is also great for other browsers other then just chrome.
Selenium = [youtube]https://www.youtube.com/watch?v=_Di0q7nB6MA&t=7s[/youtube]

Since I was unable to use Selenium I Checked out using the chrome Console. This way will open then hide the console so you don't have to deal with seeing it all the time(its like its not even there!). Then you can preform most actions you miss from IE COMs by using some basic JavaScript which I cover some basic JS in the video to get you started.
Chrome Console = [youtube]https://www.youtube.com/watch?v=2Z7X7IjHucM&list=PLfHPAKSz_DJpRd-XB71oTwN-iahluit-X&index=5&t=6s[/youtube]

In the long run I ended up finding a really nice chrome extension called Auto Control Manager. This cool tool helped me to work with AHK and Chrome in a super simple way. The great benefit to this also was a lot of things I would have normally coded into AHK I could do with in the ext with a few simple clicks and no code needed. This way was a great way to keep my AHK simple and sort.
Auto Control = [youtube]https://www.youtube.com/watch?v=gr4z0Xw8W2g&t=86s[/youtube]

Another well know way is by using Chrome.AHK which I have not done a video on but you should still check out here. Hopefully Ill get around to this one day.
Chrome.AHK = [youtube]https://www.autohotkey.com/boards/viewtopic.php?t=42890[/youtube]

Simplest Way With Out Admin Rights or Installing Anything Outside of AHK but not the best but hey if all else fails
sends =[youtube] https://www.youtube.com/watch?v=2DfaTWNkU7I&t=4s[/youtube]

Let me know if you think of any other ways you have seen AHK used with Chrome. As I add video with AHK and Chrome I will be sure to update this post as time goes on.
Last edited by tadamm on 16 Feb 2021, 16:30, edited 1 time in total.

SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Post by SOTE » 28 Jan 2021, 22:09

I think an additional option is missing, thus will fall under "Other". That is using the RunJsFromChromeAddressBar function. This has a lot of potential as a minimalist and all AHK coding method, that doesn't require 3rd party extensions. References to this option:

https://www.autohotkey.com/boards/viewtopic.php?p=326962#p326962
(Detect clickable link in Chrome)

https://www.autohotkey.com/boards/viewtopic.php?p=362634#p362634
(AHK with Chrome extension (No Selenium, no debug mode, no web connectors)

A major factor is you have to learn some JavaScript (js), in order to do different tasks or have pre-coded JavaScript in a function for a particular task dealing with web pages. There is almost no way around needing to learn some JavaScript because that is the default language of web browsers and often the code you need to automate a specific web page can be different.

You could use image search, OCR, and or shortcut keys for the web browser instead. Some competing automation tools are entirely image based. These methods are somewhat doable and usable, if the task is simple. However, those methods will be problematic for more complicated tasks (like getting certain data out of web pages), greater precision, and when trying to use the script on multiple computers.

Even if we had an Open-Source AutoHotkey extension for Chrome and Edge (this includes the 3rd party extension AutoControl), it would still require that the AHK Coder know some JavaScript and about CSS Selectors. Though I do want to make it clear that I'm in no way against a possible AutoHotkey extension, which could make things easier. AutoControl is 3rd party, Closed-Source, and requires various permissions. So the privacy concerns have to be weighed against its benefits (which I agree it has some).

Another extension/add-on to look at for Chrome, Edge, and Firefox is SelectorsHub, https://selectorshub.com/ or find it more directly like at the Chrome web store. This tool makes it much easier to find the CSS Selector, XPath, or JS Path for a visual object on a web page to be used with a script. This is similar to a feature that many RPA tools have built-in. The CSS Selector for what is wanted on the web page can then be more easily placed into the JavaScript portion of the script.

Example of the RunJsFromChromeAddressBar function

Code: Select all

ClickLink("Gmail")
Return

ClickLink(LinkText, exe := "msedge.exe")
{
	js =
	(LTrim
	(() => 
	{
		const links = document.links;
		for (let i = 0; i < links.length; i++) 
		{
			if (links[i].innerText == '%LinkText%') 
			{
				links[i].click();
				break
			}
		}
	})();
	)
	RunJsFromChromeAddressBar(js, exe)
}
   
RunJsFromChromeAddressBar(js, exe := "msedge.exe") 
{
   static WM_GETOBJECT := 0x3D
        , ROLE_SYSTEM_TEXT := 0x2A
        , STATE_SYSTEM_FOCUSABLE := 0x100000
        , SELFLAG_TAKEFOCUS := 0x1
        , AccAddrBar
   if !AccAddrBar {
      window := "ahk_class Chrome_WidgetWin_1 ahk_exe " . exe
      SendMessage, WM_GETOBJECT, 0, 1, Chrome_RenderWidgetHostHWND1, % window
      AccChrome := AccObjectFromWindow( WinExist(window) )
      AccAddrBar := SearchElement(AccChrome, {Role: ROLE_SYSTEM_TEXT, State: STATE_SYSTEM_FOCUSABLE})
   }
   AccAddrBar.accValue(0) := "javascript:" . js
   AccAddrBar.accSelect(SELFLAG_TAKEFOCUS, 0)
   ControlSend,, {Enter}, % window, Chrome Legacy Window
}

SearchElement(parentElement, params)
{
   found := true
   for k, v in params {
      try {
         if (k = "ChildCount")
            (parentElement.accChildCount != v && found := false)
         else if (k = "State")
            (!(parentElement.accState(0) & v) && found := false)
         else
            (parentElement["acc" . k](0) != v && found := false)
      }
      catch 
         found := false
   } until !found
   if found
      Return parentElement
   
   for k, v in AccChildren(parentElement)
      if obj := SearchElement(v, params)
         Return obj
}

AccObjectFromWindow(hWnd, idObject = 0) 
{
   static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
        , IID_IAccessible := "{618736E0-3C3D-11CF-810C-00AA00389B71}"
        , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
        , h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
        
   VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF
   DllCall("ole32\CLSIDFromString", "Str", idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, "Ptr", &IID)
   if DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject, "Ptr", &IID, "PtrP", pAcc) = 0
      Return ComObject(VT_DISPATCH, pAcc, F_OWNVALUE)
}

AccChildren(Acc) 
{
   static VT_DISPATCH := 9
   Loop 1  {
      if ComObjType(Acc, "Name") != "IAccessible"  {
         error := "Invalid IAccessible Object"
         break
      }
      try cChildren := Acc.accChildCount
      catch
         Return ""
      Children := []
      VarSetCapacity(varChildren, cChildren*(8 + A_PtrSize*2), 0)
      res := DllCall("oleacc\AccessibleChildren", "Ptr", ComObjValue(Acc), "Int", 0
                                                , "Int", cChildren, "Ptr", &varChildren, "IntP", cChildren)
      if (res != 0) {
         error := "AccessibleChildren DllCall Failed"
         break
      }
      Loop % cChildren  {
         i := (A_Index - 1)*(A_PtrSize*2 + 8)
         child := NumGet(varChildren, i + 8)
         Children.Push( (b := NumGet(varChildren, i) = VT_DISPATCH) ? AccQuery(child) : child )
         ( b && ObjRelease(child) )
      }
   }
   if error
      ErrorLevel := error
   else
      Return Children.MaxIndex() ? Children : ""
}

AccQuery(Acc) 
{
   static IAccessible := "{618736e0-3c3d-11cf-810c-00aa00389b71}", VT_DISPATCH := 9, F_OWNVALUE := 1
   try Return ComObject(VT_DISPATCH, ComObjQuery(Acc, IAccessible), F_OWNVALUE)
}

burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Post by burque505 » 20 Feb 2021, 19:10

@SOTE, good call. I use RunJsFromChromeAddressBar and SelectorsHub both quite a bit. SeleniumBasic 3.141.0.0 is an option I'm working on, you might have a look here.

william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

Re: Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Post by william_ahk » 21 Feb 2021, 07:37

I've been using using puppeteer/playwright lately and it worked really well.

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Post by gregster » 21 Feb 2021, 08:46

william_ahk wrote:
21 Feb 2021, 07:37
I've been using using puppeteer/playwright lately
with AutoHotkey (which would the subject of this topic) ? I thought that would rather be a node.js thing.

But afaik, Puppeteer depends on the same chrome debugging protocol like Chrome.ahk and Selenium in order to automate Chrome.
The individual syntaxes of the front-end packages obviously differ... especially Chrome.ahk could use a few more high-level methods to choose from. Currently, I still have to dig around a lot in the protocol docs at Google, if I want to do something browser-specific (javascript execution is more straightforward, though).

william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

Re: Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Post by william_ahk » 22 Feb 2021, 05:43

gregster wrote:
21 Feb 2021, 08:46
william_ahk wrote:
21 Feb 2021, 07:37
I've been using using puppeteer/playwright lately
with AutoHotkey (which would the subject of this topic) ? I thought that would rather be a node.js thing.

But afaik, Puppeteer depends on the same chrome debugging protocol like Chrome.ahk and Selenium in order to automate Chrome.
The individual syntaxes of the front-end packages obviously differ... especially Chrome.ahk could use a few more high-level methods to choose from. Currently, I still have to dig around a lot in the protocol docs at Google, if I want to do something browser-specific (javascript execution is more straightforward, though).
Yes it's a node.js thing. Not the most memory efficient but it's a lot faster than Chrome.ahk (which is quite fascinating as well). I think the Google team is doing some black magic since it's their browser.

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Post by gregster » 22 Feb 2021, 06:15

william_ahk wrote:
22 Feb 2021, 05:43
Yes it's a node.js thing. Not the most memory efficient but it's a lot faster than Chrome.ahk (which is quite fascinating as well). I think the Google team is doing some black magic since it's their browser.
Yeah, Chrome.ahk has probably a few speed bottlenecks in terms of its websocket and json interfaces - but at least for the latter there is an alternative version by teadrinker which should be a bit faster.
And yes, the underlying chrome debugging protocol (created by Google) is huge and covers a lot of things, much more than simple DOM manipulation. But since the new Edge and Opera also support it (both are also Chromium-based) it's great to be able to use the same syntax for their automation.

william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

Re: Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Post by william_ahk » 26 Feb 2021, 02:11

gregster wrote:
22 Feb 2021, 06:15
Yeah, Chrome.ahk has probably a few speed bottlenecks in terms of its websocket and json interfaces - but at least for the latter there is an alternative version by teadrinker which should be a bit faster.
And yes, the underlying chrome debugging protocol (created by Google) is huge and covers a lot of things, much more than simple DOM manipulation. But since the new Edge and Opera also support it (both are also Chromium-based) it's great to be able to use the same syntax for their automation.
About the the alternative version by teadrinker, are you referring to RunJsFromChromeAddressBar or is there a more direct way of interacting with devtools protocol using AHK?

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Post by gregster » 26 Feb 2021, 04:53

william_ahk wrote:
26 Feb 2021, 02:11
gregster wrote:
22 Feb 2021, 06:15
Yeah, Chrome.ahk has probably a few speed bottlenecks in terms of its websocket and json interfaces - but at least for the latter there is an alternative version by teadrinker which should be a bit faster.
And yes, the underlying chrome debugging protocol (created by Google) is huge and covers a lot of things, much more than simple DOM manipulation. But since the new Edge and Opera also support it (both are also Chromium-based) it's great to be able to use the same syntax for their automation.
About the the alternative version by teadrinker, are you referring to RunJsFromChromeAddressBar or is there a more direct way of interacting with devtools protocol using AHK?
That is also a very useful function, but in this case I meant a version of Chrome.ahk in which teadrinker replaced the json component to make it faster. I think this is the latest:
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=42890&p=376782#p382756

User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Multiple Ways to Automate Chrome with AHK. Selenium, Console, Chrome ext. Chrome.AHK

Post by Xeo786 » 27 Apr 2022, 01:28

Please add Rufaydium to poll
viewtopic.php?f=6&t=102616
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

Post Reply

Return to “Tutorials (v1)”