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

Post your working scripts, libraries and tools
GeekDude
Posts: 838
Joined: 02 Oct 2013, 22:13

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

16 Jan 2018, 17:38

Chrome.ahk

Automate Google Chrome using native AutoHotkey.


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
    • Launching in headless mode is not currently supported by this library
  • 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] [Expand] [Download] GeSHi © Codebox Plus


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




Releases
User avatar
gregster
Posts: 1382
Joined: 30 Sep 2013, 06:48

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

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: 279
Joined: 03 May 2014, 14:50
GitHub: Run1e

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

16 Jan 2018, 18:23

Pretty cool, I'll also have a play with it.
User avatar
Joe Glines
Posts: 551
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

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

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! :)
GeekDude
Posts: 838
Joined: 02 Oct 2013, 22:13

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

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: 1242
Joined: 01 Oct 2013, 14:57

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

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
GeekDude
Posts: 838
Joined: 02 Oct 2013, 22:13

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

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: 1242
Joined: 01 Oct 2013, 14:57

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

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]
GeekDude
Posts: 838
Joined: 02 Oct 2013, 22:13

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

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: 551
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

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

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...
GeekDude
Posts: 838
Joined: 02 Oct 2013, 22:13

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

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: 551
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

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

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?
GeekDude
Posts: 838
Joined: 02 Oct 2013, 22:13

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

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] [Download] GeSHi © Codebox Plus

#Include Chrome.ahk

TabInst := Chrome.GetTab()
TabInst.Evaluate("alert('hi!');")
User avatar
Joe Glines
Posts: 551
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

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

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:
tmplinshi
Posts: 1242
Joined: 01 Oct 2013, 14:57

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

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: 6440
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

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

25 Jan 2018, 08:57

Headless chrome automation you say... interesting :think: :+1:
DanielToward13
Posts: 63
Joined: 18 May 2017, 10:56

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

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?

Return to “Scripts and Functions”

Who is online

Users browsing this forum: ArchieEU and 14 guests