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

Post your working scripts, libraries and tools
SuperJames
Posts: 1
Joined: 28 Apr 2018, 10:15
GitHub: SuperAvarice

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

12 Oct 2018, 16:35

Thanks for the library GeekDude!

My question: Can I run multiple instances of this class? I would like to have many controllable windows open at the same time.

I tried this, but failed.

Code: Select all

#include <External\Chrome>

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

FileCreateDir, ChromeProfile2
ChromeInst_1 := new Chrome("ChromeProfile2")

; 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()
PageInstance.Evaluate("alert('Hello World!');")

PageInstance_1 := ChromeInst_1.GetPage()
PageInstance_1.Call("Page.navigate", {"url": "https://google.com/"})
PageInstance_1.WaitForLoad()
PageInstance_1.Evaluate("alert('Hello World 2!');")

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

PageInstance_1.Call("Browser.close")
PageInstance_1.Disconnect()

ExitApp
return
chngrcn
Posts: 129
Joined: 29 Feb 2016, 08:55

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

17 Oct 2018, 07:14

Code: Select all

FileAppend % PageInstance.Evaluate("document.documentElement.outerHTML"), C:\Test.txt
when I tried this code, it did not return the page's HTML code to the "test.txt" file. How should I try a code to get the page's HTML code?
User avatar
Xtra
Posts: 1239
Joined: 02 Oct 2015, 12:15

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

17 Oct 2018, 10:43

PageInstance.Evaluate("document.getElementsByTagName('html')[0].textContent;").Value
User avatar
jeeswg
Posts: 5265
Joined: 19 Dec 2016, 01:58
Location: UK

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

30 Oct 2018, 22:25

- I have been experimenting with Chrome.ahk for the first time, and have two queries re. my script below:
- Is there a way to make the tab visible when creating an instance. (And create multiple tabs on one window.)
- Is there a way to list all tabs. Thanks.
[code]
#Include ../Chrome.ahk

q:: ;run Chrome in debug mode
Run, chrome.exe --remote-debugging-port=9222
return

w:: ;attempt at listing all tabs (only listed one tab)
for vKey, oPage in Chrome.GetPageList()
MsgBox, % oPage.type "`r`n" oPage.title "`r`n" oPage.url

PageInst := Chrome.GetPage(1)
vUrl := PageInst.Evaluate("document.URL;").Value
;vUrl := PageInst.Evaluate("window.location.href;").Value
vTitle := PageInst.Evaluate("document.title;").Value
MsgBox, % vTitle "`r`n" vUrl
return

e:: ;open a tab (but tab was not visible)
ChromeInst := new Chrome("ChromeProfile")
if !(PageInst := ChromeInst.GetPage())
{
MsgBox, Could not retrieve page!
ChromeInst.Kill()
}
else
{
PageInst.Call("Page.navigate", {"url": "https://autohotkey.com/"})
PageInst.WaitForLoad()
vUrl := PageInst.Evaluate("document.URL;").Value
;vUrl := PageInst.Evaluate("window.location.href;").Value
vTitle := PageInst.Evaluate("document.title;").Value
MsgBox, % vTitle "`r`n" vUrl
try
PageInst.Call("Browser.close") ; Fails when running headless
catch
ChromeInst.Kill()
PageInst.Disconnect()
}
return
[/code]

Return to “Scripts and Functions”

Who is online

Users browsing this forum: gwarble, TAC109, wsimon98 and 49 guests