Automate Google Chrome using native AutoHotkey
Automate Google Chrome using native AutoHotkey.
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/
Page.printToPDF
Page.captureScreenshot
To start using this library you need to create an instance of the class
Chrome.
Chrome's constructor accepts four optional parameters:
about:blank.
9222will be used as specified in the Chrome DevTools Protocol documentation.
Once an instance of the class
Chromehas 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.
#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 PageInst := ChromeInst.GetPage() PageInst.Call("Page.navigate", {"url": "https://autohotkey.com/"}) PageInst.WaitForLoad()
; Execute some JavaScript PageInst.Evaluate("alert('Hello World!');")
; Close the browser (note: this closes all pages/tabs) PageInst.Call("Browser.close") PageInst.Disconnect()
ExitApp return
You can find more sample code showing how to use this library in the Examples folder.