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

Post your working scripts, libraries and tools for AHK v1.1 and older
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

Post by geek » 16 Jan 2018, 17:38

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

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

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

Post by gregster » 16 Jan 2018, 17:49

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.

User avatar
runie
Posts: 304
Joined: 03 May 2014, 14:50
Contact:

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

Post by runie » 16 Jan 2018, 18:23

Pretty cool, I'll also have a play with it.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

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

Post by Xtra » 16 Jan 2018, 18:52

Thanks for sharing!

User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

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

Post by Joe Glines » 17 Jan 2018, 06:27

:bravo: I'm very excited to play with this! Thanks GeekDude!
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!

User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

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

Post by Joe Glines » 17 Jan 2018, 06:42

@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! :)
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!

geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

Post by geek » 17 Jan 2018, 07:39

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

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

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

Post by tmplinshi » 17 Jan 2018, 09:21

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

geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

Post by geek » 17 Jan 2018, 12:05

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.

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

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

Post by tmplinshi » 21 Jan 2018, 02:26

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]

geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

Post by geek » 21 Jan 2018, 12:08

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?

User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

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

Post by Joe Glines » 23 Jan 2018, 08:57

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...
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!


geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

Post by geek » 23 Jan 2018, 17:29

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!

User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

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

Post by Joe Glines » 23 Jan 2018, 19:44

@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?
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!

geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

Post by geek » 23 Jan 2018, 20:33

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!');")

User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

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

Post by Joe Glines » 23 Jan 2018, 21:42

: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:
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

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

Post by tmplinshi » 24 Jan 2018, 12:00

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.

User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

Post by joedf » 25 Jan 2018, 08:57

Headless chrome automation you say... interesting :think: :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

DanielToward13
Posts: 74
Joined: 18 May 2017, 10:56

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

Post by DanielToward13 » 25 Jan 2018, 16:06

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?

Post Reply

Return to “Scripts and Functions (v1)”