[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: 140
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: 1269
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: 5411
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: Select all

#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
Gh0sTG0
Posts: 6
Joined: 25 Jun 2018, 07:58

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

19 Nov 2018, 08:52

Hi! TY for nice lib. I have one problem with it.
I'm using A:=PageInst.Evaluate("document.getElementsByClassName...").Value and sometimes it stuck.
When I double click script in tray it's window is full of 244: While,!this.responses[ID] 245: Sleep,50 (0.05).
So, I find that place in script and... where's your safety for if page NOT responses?
Added some safety into it, from 242:

Code: Select all

; Wait for the response
			this.responses[ID] := False
			Flag := 0
			while !this.responses[ID]
			{
				Flag += 1
				Sleep, 50
				If (Flag > 100)
				{
					throw Exception("Chrome lag and not sending responce")
					Return
				}
			}
This will try that for 5 seconds and return Error if chrome lags
PS 1) I tried waiting. I left it stuck for ~1hour and nothing
2) I had this stuck earlier. And now, after I made clean reinstall of my win10x64 and clean install of Chrome

PPS can someone check my code for correct working?
Gh0sTG0
Posts: 6
Joined: 25 Jun 2018, 07:58

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

24 Nov 2018, 15:37

Next thing I find: sometime when I use PageInst := Chrome.GetPage(1) it stuck too.
I add some Tooltips, X where X is number, so I know, that ahk shows me ToolTip 1 just before getpage. Then there's tooltip 2, that not shows

Return to “Scripts and Functions”

Who is online

Users browsing this forum: [Shambles], Drugwash, robmar-zl, robodesign and 69 guests