[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: 1298
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: 5760
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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
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
r2997790
Posts: 40
Joined: 02 Feb 2017, 02:46

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

29 Dec 2018, 05:58

Hi everyone

I'm trying to hide some elements, by TagName or Class Name, but I seem to have the syntax wrong. Could someone please help me understand the right syntax with this super AHK class from @geekdude? Thank you!

PageInstance.Evaluate(document.getElementsByTagName("h1").style.display = none)

PageInstance.Evaluate(document.getElementsByTagName("h1")[0].style.display = none)

PageInstance.Evaluate(document.getElementsByClassName("example").style.display = none)

PageInstance.Evaluate(document.getElementsByClassName("example")[1].style.display = none)

Thanks!
teadrinker
Posts: 494
Joined: 29 Mar 2015, 09:41
Contact:

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

29 Dec 2018, 06:23

Hi r2997790

When you use Evaluate() you must pass a javascript-code string and keep right js-syntax. This should work:

Code: Select all

PageInstance.Evaluate("document.getElementsByTagName('h1')[0].style.display = 'none';")
r2997790
Posts: 40
Joined: 02 Feb 2017, 02:46

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

29 Dec 2018, 07:19

teadrinker wrote:
29 Dec 2018, 06:23
Hi r2997790

When you use Evaluate() you must pass a javascript-code string and keep right js-syntax. This should work:

Code: Select all

PageInstance.Evaluate("document.getElementsByTagName('h1')[0].style.display = 'none';")
--- Works like a dream! Thank you @teadrinker!
User avatar
adegard
Posts: 76
Joined: 24 Nov 2017, 05:58
GitHub: adegard

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

31 Dec 2018, 06:12

Image

I have this issue... What is the problem here?
Thanks
SupposedToBeWorking
Posts: 25
Joined: 08 Mar 2017, 16:15

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

17 Jan 2019, 11:59

I can not figure out what I am doing wrong to get values from elements in Chrome. Any help is appreciated!

Code: Select all


#Persistent
#SingleInstance, force
#NoEnv
SetWorkingDir, %A_ScriptDir%
SetBatchLines, -1
#Include Chrome.ahk

url=https://teams.microsoft.com

FileCreateDir, ChromeProfile
ChromeInst := new Chrome("ChromeProfile" , url, "")
PageInst := ChromeInst.GetPageByURL(url,,, "Callback")

Loop
{
	MsgBox, % PageInst.Evaluate("document.getElementsByTagName('html')[0].textContent;").Value
	Sleep 1000
}


Return

Msgbox is always empty.


EDIT

I did the following and it worked.

Code: Select all

#Persistent
#SingleInstance, force
#NoEnv
SetWorkingDir, %A_ScriptDir%
SetBatchLines, -1
#Include Chrome.ahk

url=https://teams.microsoft.com

; OPEN CHROME AND CONNECT TO PAGE 
FileCreateDir, ChromeProfile
ChromeInst := new Chrome("ChromeProfile" , url, "")
Try
{
	Sleep 5000
	PageInst := ChromeInst.GetPageByURL(url,,, "Callback")
}
Catch
{
	Sleep 5000
	PageInst := ChromeInst.GetPageByURL(url,,, "Callback")
}
PageInst.Call("Console.enable") 						;watch the console for messages served via javascript
if !(PageInst := ChromeInst.GetPage())
{
	MsgBox, Could not retrieve page!
	ChromeInst.Kill()
}
else
{
	PageInst.WaitForLoad()
}



clickCreateSessionDate = 
(
	var allPostsArrayLength = document.querySelectorAll(".ts-message").length
	var allPostsArrayLength = allPostsArrayLength-1
	var mostRecentPostText = document.querySelectorAll(".ts-message")[allPostsArrayLength].children[0].innerHTML
	alert(mostRecentPostText)
)
msgbox, ready?

Loop, 5
{
	MsgBox, % PageInst.Evaluate("document.querySelectorAll('.ts-message')[1].children[0].innerHTML;").Value
	PageInst.Evaluate(clickCreateSessionDate)
	Sleep 1000
}
ExitApp
The difference is in these lines:

Code: Select all

ChromeInst := new Chrome("ChromeProfile" , url, "")
Try
{
	Sleep 5000
	PageInst := ChromeInst.GetPageByURL(url,,, "Callback")
}
Catch
{
	Sleep 5000
	PageInst := ChromeInst.GetPageByURL(url,,, "Callback")
}
PageInst.Call("Console.enable") 						;watch the console for messages served via javascript
if !(PageInst := ChromeInst.GetPage())
{
	MsgBox, Could not retrieve page!
	ChromeInst.Kill()
}
else
{
	PageInst.WaitForLoad()
}
Stavencross
Posts: 62
Joined: 24 May 2016, 16:42

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

20 Jan 2019, 09:45

SuperJames wrote:
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
Its been a bit since I played with chrome.ahk but yes you can run up to 5 instances of the class before it starts to fatal err on you ( +/- 1, it gets finicky)

If I recall correctly - you don't need to call a second chrome profile

Code: Select all

ChromeInst_1 := new Chrome("ChromeProfile2")
try

Code: Select all

ChromeInst_1 := new Chrome("ChromeProfile")
Not sure this is the full answer you need but it might start you on the right path

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 25 guests