Page 1 of 34

[Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Posted: 16 Jan 2018, 17:38
by geek
Chrome.ahk

Automate Google Chrome using native AutoHotkey.


Watch the full playlist on YouTube

How it works

Chrome offers a WebSocket based API they call the Chrome DevTools Protocol. This API is what allows web development tools to build integrations, and tools such as Selenium to perform their automation. The protocol's documentation describes a plethora of exciting endpoints accessible using this library, and can be found at the link below.

https://chromedevtools.github.io/devtools-protocol/


Advantages
  • No external dependencies such as Selenium are required
  • Chrome can be automated even when running in headless mode
  • Chrome consistently benchmarks better than Internet Explorer
  • Chrome offers extensions which provide unique opportunities for interaction
    • Automate your Chromecast
    • Connect to remote servers with FoxyProxy and update web based configs
    • Manage your password vault with LastPass
  • Many features are available that would be difficult to replicate in Internet Explorer
    • Page.printToPDF
    • Page.captureScreenshot
    • Geolocation spoofing

Limitations
  • Chrome must be started in debug mode
  • If chrome is already running out of debug mode, it must either be closed and reopened or launched again under a new profile that isn't already running
  • You cannot attach to an existing non-debug session
  • Less flexible than Internet Explorer's COM interface
    • Cannot pass function references for callbacks

Using this Library

To start using this library you need to create an instance of the class Chrome. Chrome's constructor accepts four optional parameters:

1. ProfilePath - This is the path, relative to the working directory, that your Chrome user profile is located. If an empty folder is given, chrome will generate a new user profile in it. When this parameter is omitted, Chrome will be launched under the default user profile. However, if chrome is already running under that user profile out of debug mode, this will fail. Because of this, it is recommended to always launch Chrome under an alternate user profile.
2. URL - The page that chrome should initially be opened to. Pass an empty string to open Chrome's homepage. When this parameter is omitted, Chrome will be opened to about:blank.
3. ChromePath - The path to find the Chrome executable file. When this parameter is omitted, Chrome will be launched from the path in its start menu entry.
4. DebugPort - The network port to communicate with Chrome over. When this parameter is omitted, port 9222 will be used as specified in the Chrome DevTools Protocol documentation.

Once an instance of the class Chrome has been created, Google Chrome will be launched. To connect to the newly opened page call PageInstance := ChromeInstance.GetPage(). Afterward, use PageInstance.Call() to call protocol endpoints, and PageInstance.Evaluate() to execute JavaScript.

Code: Select all

#Include Chrome.ahk

; Create an instance of the Chrome class using
; the folder ChromeProfile to store the user profile
FileCreateDir, ChromeProfile
ChromeInst := new Chrome("ChromeProfile")

; Connect to the newly opened tab and navigate to another website
; Note: If your first action is to navigate away, it may be just as
; effective to provide the target URL when instantiating the Chrome class
PageInstance := ChromeInst.GetPage()
PageInstance.Call("Page.navigate", {"url": "https://autohotkey.com/"})
PageInstance.WaitForLoad()

; Execute some JavaScript
PageInstance.Evaluate("alert('Hello World!');")

; Close the browser (note: this closes *all* pages/tabs)
PageInstance.Call("Browser.close")
PageInstance.Disconnect()

ExitApp
return
You can find more sample code showing how to use this library in the Examples folder.


Releases

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 16 Jan 2018, 17:49
by gregster
Very interesting. Thanks a lot for this library. I will have to play with it...
Btw... I also like your CodeQuicktester a lot and use it everyday.

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 16 Jan 2018, 18:23
by runie
Pretty cool, I'll also have a play with it.

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 16 Jan 2018, 18:52
by Xtra
Thanks for sharing!

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 17 Jan 2018, 06:27
by Joe Glines
:bravo: I'm very excited to play with this! Thanks GeekDude!

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 17 Jan 2018, 06:42
by Joe Glines
@GeekDude- A couple of notes
1) I just got my first one to launch! :)
2) When I downloaded the various zipped files from GitHub, the two subfolders (AutoHotkey-JSON and WebSocket.ahk) were empty.
3) After I went back to GitHub and got them, the Include from Chrome.ahk had an issue on line 172 (the #Include for WebSocket)

Thanks again for your work on this! It can definitely open an amazing amount of opportunities for AutoHotkey! :)

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 17 Jan 2018, 07:39
by geek
Make sure you're downloading the release file "Chrome.ahk_v1.0.zip" from the releases page, not the source code. As you have learned, GitHub does not include submodules (i.e. AutoHotkey-JSON and WebSocket.ahk) in its source downloads. The release has all the #Include's built in (no lib folder needed :) ).

Image

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 17 Jan 2018, 09:21
by tmplinshi
Excellent! However I can't get it working. I got a SecurityError from IE popup window. Adding "WB.Silent := True" to Chrome.ahk fixed the error, but the example script stucked at code:
while, !this.Connected
Sleep, 50

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 17 Jan 2018, 12:05
by geek
What did the security error dialog say? If it was a problem that prevented the websocket form connecting, it would make sense that it would get stuck waiting for the connection to complete.

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 21 Jan 2018, 02:26
by tmplinshi
The error dialog wrote: ---------------------------
Script Error
---------------------------
An error has occurred in the script on this page.

Line: 1
Char: 1
Error: SecurityError
Code: 0
URL:

Do you want to continue running scripts on this page?
---------------------------
[Yes] [No]

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 21 Jan 2018, 12:08
by geek
Ah, hm. This script works by creating a hidden web control (IE) and using Microsoft's WebSocket object through COM. It then uses this WebSocket to connect to Chrome. On my system, IE is forbidden from accessing localhost, but is allowed to access 127.0.0.1. It looks like your system is blocking 127.0.0.1 too, for some reason.

You can disable this block (not sure how this effects general security of IE) by following the steps in this StackOverflow post. From my testing, only that top checkbox (intranet) needed to be unchecked to allow access (to localhost), but it may be different for you. I'm not sure how this change would effect security, so I would only do this for testing purposes.

What version of Windows and Internet Explorer do you have, and are you on a corporate network? If you are, what kind of proxy settings are set in IE?

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 23 Jan 2018, 08:57
by Joe Glines
I've been playing with this & it is pretty awesome! I've adapted it to use QuerySelectorAll (so items that are an array can be specified) I've also worked-through getting the OuterHTML (however there doesn't seem to be a way to get InnerText)

Overall pretty cool @GeekDude! I was curious though about one thing though the intro states
You cannot attach to an existing non-debug session
Is there no way to work around this? Being able to connect to a current existence is really beneficial...

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 23 Jan 2018, 11:01
by guest3456
very nice

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 23 Jan 2018, 17:29
by geek
Joe Glines wrote:I've been playing with this & it is pretty awesome! I've adapted it to use QuerySelectorAll (so items that are an array can be specified) I've also worked-through getting the OuterHTML (however there doesn't seem to be a way to get InnerText)
Great! I'm glad to hear you like it. Feel free to share any useful tips you come up with :)
Joe Glines wrote:Overall pretty cool @GeekDude! I was curious though about one thing though the intro states
You cannot attach to an existing non-debug session
Is there no way to work around this? Being able to connect to a current existence is really beneficial...
As far as I have been able to there really is no real way to turn a non-debug chrome instance to a debug instance without first closing out all windows under that profile. As a workaround, you could change the shortcut you open Chrome with to include the debug flag so that you'll always be opening in debug mode :).

I'm not sure what kind of security risks that would open you up to. If Chrome allows non-localhost connections (I think it might) and is allowed through the firewall (probably is) anyone else on the network could sneak into your session without you knowing. I'd need to do some more testing before I can recommend this in good conscience.
guest3456 wrote:very nice
Thanks!

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 23 Jan 2018, 19:44
by Joe Glines
@GeekDudeBecause I already had a shortcut to launch Chrome in debug mode for Selenium (so I could connect to it as tmplinshi did here.

Perhaps I need to control for the profile?

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 23 Jan 2018, 20:33
by geek
If you have a chrome instance already running in debug mode, you can skip the initialization of the Chrome class and just call Chrome.GetTab() directly. For example:

Code: Select all

#Include Chrome.ahk

TabInst := Chrome.GetTab()
TabInst.Evaluate("alert('hi!');")

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 23 Jan 2018, 21:42
by Joe Glines
:dance: Thanks GeekDude! I'd tried that however I'd failed to close-out the background /default version of Chrome that stays running. After closing that version out, it was able to connect! I'll work on cleaning up the examples I've created and will share tomorrow/soon.

Thanks again for working on this! It opens up a ton of possibilities! :dance:

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 24 Jan 2018, 12:00
by tmplinshi
GeekDude wrote:Ah, hm. This script works by creating a hidden web control (IE) and using Microsoft's WebSocket object through COM. It then uses this WebSocket to connect to Chrome. On my system, IE is forbidden from accessing localhost, but is allowed to access 127.0.0.1. It looks like your system is blocking 127.0.0.1 too, for some reason.

You can disable this block (not sure how this effects general security of IE) by following the steps in this StackOverflow post. From my testing, only that top checkbox (intranet) needed to be unchecked to allow access (to localhost), but it may be different for you. I'm not sure how this change would effect security, so I would only do this for testing purposes.

What version of Windows and Internet Explorer do you have, and are you on a corporate network? If you are, what kind of proxy settings are set in IE?
Sorry for late reply, and thanks for your help, unchecking the second checkbox "Include all sites that bypass the proxy server" works for me.
I am using Windows 10 x64 with IE 11, on a normal network. The IE browser was set with PAC proxy, which is used by Shadowsocks.

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 25 Jan 2018, 08:57
by joedf
Headless chrome automation you say... interesting :think: :+1:

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Posted: 25 Jan 2018, 16:06
by DanielToward13
Thanks GeekDude. That’s great!
is there a way to get the scroll bar position in order to know where in the page the current view is?