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

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

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

Post by Xeo786 » 12 Aug 2022, 09:53

WallNuts wrote:
12 Aug 2022, 09:11
Xeo786 wrote:
12 Aug 2022, 02:22
try msgbox, % vResponsive.value
Thank you for the help Xeo786! Unfortunately neither
msgbox, % vResponsive.value
nor
msgbox, % vResponsive.checked
worked. Both still sent a blank msgbox.
if the chrome console returns some result using that same JS code then it should return the same result with evaluate() method.
make sure to use that JS on most outer / Parent frame in chrome console.
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

WallNuts
Posts: 3
Joined: 11 Aug 2022, 17:01

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

Post by WallNuts » 12 Aug 2022, 10:48

Xeo786 wrote:
12 Aug 2022, 09:53
WallNuts wrote:
12 Aug 2022, 09:11
Xeo786 wrote:
12 Aug 2022, 02:22
try msgbox, % vResponsive.value
Thank you for the help Xeo786! Unfortunately neither
msgbox, % vResponsive.value
nor
msgbox, % vResponsive.checked
worked. Both still sent a blank msgbox.
if the chrome console returns some result using that same JS code then it should return the same result with evaluate() method.
make sure to use that JS on most outer / Parent frame in chrome console.
Thanks for the follow up. I thought that would be the case too. As you can see below, running the code in the 'top'/parent frame in the chrome console returns 'true' but unfortunately I cannot access it in the vResponsive variable. I will try to think of another way to check if the check boxes are checked.

Image

tester
Posts: 84
Joined: 10 Jun 2021, 23:03

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

Post by tester » 13 Aug 2022, 00:21

Hello,

Is this AutoHotkey v2 compatible? If not, is there a compatible version?

Yogi
Posts: 12
Joined: 06 Jul 2022, 23:03

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

Post by Yogi » 17 Aug 2022, 21:37

Xeo786 wrote:
11 Aug 2022, 03:52
Yogi wrote:
07 Aug 2022, 19:49
Can you run chrome:

1) in the background
and
2) take a screenshot of a webpage using this without ever seeing the application?

Browny points for being able to tell it the region to capture.

I liked the IE.Visible = False command to get quick updates on things without launching the browser but sites don't really support it anymore.
You need to Run chrome with headless parameter.

Code: Select all

Chrome := new Chrome(ProfileFolder,URL,"--headless")

and you need to call Page.captureScreenshot it will return Base64-encoded image which you need to decode and save.
Thank you very much!

bezet
Posts: 7
Joined: 22 Mar 2022, 07:43

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

Post by bezet » 26 Aug 2022, 03:14

Naits55 wrote:
17 Feb 2022, 21:16
Hello guys,

I'm new to ahk and chrome.ahk... so I hope this question isn't too basic for you guys.

I have read trough the entire thread multiple times, watched all the videos and googled the crap out of this issue I have. But I'm unable to find a solution that works.

So I'm trying to enter a searchterm into a searchbox. The searchbox doesn't have an submit button, only way to activate it is by pressing enter on the keyboard.

I managed to populate the searchbox with:

Code: Select all

document.querySelector(".jss618").innerText = "Example Search Term"
Now I need to press enter...

First problem is, that I can only manually select the searchbox. I didn't find a way of putting my cursor into the box. I tried:

Code: Select all

document.querySelector(".jss618").focus()
document.querySelector(".jss618").click()
are there any other options?

Also I tried manually selecting the box (just for testing) and sending enter key stroke through the console with code like this:

Code: Select all

page.Call("Input.dispatchKeyEvent", {"type" : "rawKeyDown","windowsVirtualKeyCode" : 13,"unmodifiedText" : "\r","text" : "\r"})
I tried all kinds of different types for this event and combinations I could think of.

Only thing that works is this:

Code: Select all

page.Call("Input.dispatchKeyEvent", {"type" : "char", "text" : "Search Example"})
This will write into the searchbox. But only if i select it manually before sending this line. And also I can only submit by manually pressing enter. Which defeats the purpose of the whole thing.

So does anyone have an idea what I'm doing wrong or what else I could try?

Thanks in advance!
Is there a chance the website is public? Sound like a problem I have before.

User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

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

Post by Xeo786 » 26 Aug 2022, 04:55

bezet wrote:
26 Aug 2022, 03:14
Naits55 wrote:
17 Feb 2022, 21:16
Hello guys,

I'm new to ahk and chrome.ahk... so I hope this question isn't too basic for you guys.

I have read trough the entire thread multiple times, watched all the videos and googled the crap out of this issue I have. But I'm unable to find a solution that works.

So I'm trying to enter a searchterm into a searchbox. The searchbox doesn't have an submit button, only way to activate it is by pressing enter on the keyboard.

I managed to populate the searchbox with:

Code: Select all

document.querySelector(".jss618").innerText = "Example Search Term"
Now I need to press enter...

First problem is, that I can only manually select the searchbox. I didn't find a way of putting my cursor into the box. I tried:

Code: Select all

document.querySelector(".jss618").focus()
document.querySelector(".jss618").click()
are there any other options?

Also I tried manually selecting the box (just for testing) and sending enter key stroke through the console with code like this:

Code: Select all

page.Call("Input.dispatchKeyEvent", {"type" : "rawKeyDown","windowsVirtualKeyCode" : 13,"unmodifiedText" : "\r","text" : "\r"})
I tried all kinds of different types for this event and combinations I could think of.

Only thing that works is this:

Code: Select all

page.Call("Input.dispatchKeyEvent", {"type" : "char", "text" : "Search Example"})
This will write into the searchbox. But only if i select it manually before sending this line. And also I can only submit by manually pressing enter. Which defeats the purpose of the whole thing.

So does anyone have an idea what I'm doing wrong or what else I could try?

Thanks in advance!
Is there a chance the website is public? Sound like a problem I have before.
following code will submit text and press enter

Code: Select all

page.evaluate("document.querySelector('.jss618').focus()") ; element into focus
page.Call("Input.dispatchKeyEvent", {"text" : "Search Example`n"}) ; `n will be translated to enter some web pages detect "`r" instead
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

jekko1976
Posts: 97
Joined: 10 Oct 2014, 07:03

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

Post by jekko1976 » 03 Sep 2022, 06:29

Hello, big trouble with Chrome.ahk.
The script loops in this position and I don't know what to do.
I saw that there is an issue to this problem unresolved.

319: fnCallback := this.fnCallback
320: if (newData := %fnCallback%(data))
323: if this.responses.HasKey(data.ID)
i tried this workaround with no luck...

https://github.com/G33kDude/Chrome.ahk/issues/11

Can somebody help?

LegzRwheelz
Posts: 56
Joined: 04 Nov 2019, 17:46

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

Post by LegzRwheelz » 04 Sep 2022, 09:36

Hi, this looks promising. I use AHK extensively, in combination with my remote on my HTPC for all sorts of hotkeys. My current focus involves chrome, launching YouTube on TV url using a Smart TV user agent string, in kiosk mode. Upon hotkey release, it draws a black screen, hides my desktop icons, hides the taskbar, moves my mouse to the top right, changes all cursors to a fully transparent cursor, activate SMS text entry mode for my remote control to search for videos and lastly navigation sounds upon button presses, it is, literally the same as a Samsung or LG smart TV version of YouTube. When I close chrome using alt+f4, everything reverts back to the pre-launch state, except for the tab that launches upon the HK press. This is the only real issue I have and if I am not mindful, I could have 40+ tabs open in a very short period of time. I can't just create a new button that sends "ctrl+w" to close the tab because all of my remote keys are occupied and everything I run, can be closed using alt+f4. So I have been trying to find a way to close chrome by closing the tab, as closing the only tab open will essentially be the same as alt+f4. In my search of attempting to bend chrome to my will, I discovered this Library and then I started searching for a way to close the tab using DevTools and found THIS-->https://developer.chrome.com/docs/extensions/reference/tabs/. Can this be used with this library? Something like:

Code: Select all

IfWinActive, YouTube on TV - Google Chrome
     !f4::
     chrome.tabs.remove(
  tabIds: number | number[],
  callback?: function,
)
return

IDK if that is even proper usage, but it is the idea that I've brainstormed. Is it doable? if so, can someone please help me with this ? If not would anybody happen to have any other suggestions on the AHK side of things to close the tab with alt+f4 ONLY if YouTube on TV is open?


aTdHvAaNnKcSe<----Thanks in advance 🤪


EDIT
I solved my issue and it was quite simple actually. I didn't need to try anything extras besides signing out of my portable install of chrome so it doesn't sync with my main installation then set chrome to always open a new tab upon open and added the argument --New-Window in my batch file. it works flawlessly now

tester
Posts: 84
Joined: 10 Jun 2021, 23:03

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

Post by tester » 09 Sep 2022, 22:31

william_ahk wrote:
20 Jul 2021, 21:35
Is it possible to suppress the websocket error popup?
It seems it requires modifying the library code,

Code: Select all

                WB.Navigate("about:<!DOCTYPE html><meta http-equiv='X-UA-Compatible'"
                . "content='IE=edge'><body></body>")
to

Code: Select all

                WB.silent := true	; < -- this seems to work!
                WB.Navigate("about:<!DOCTYPE html><meta http-equiv='X-UA-Compatible'"
                . "content='IE=edge'><body></body>")

william_ahk
Posts: 481
Joined: 03 Dec 2018, 20:02

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

Post by william_ahk » 10 Sep 2022, 02:05

@tester Tysm! Wish I had known this sooner

User avatar
neovis
Posts: 34
Joined: 12 Jun 2022, 03:56
Location: Republic of Korea
Contact:

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

Post by neovis » 20 Sep 2022, 11:15

Hello, I recently started using it. This is very useful.
However, using the JavaScript object is very difficult, so I added code to wrap the RemoteObject and use it as a native object.
ChromeRemoteObject class automatically adds an eval method to Chrome.Page.eval.

Source on Github: https://github.com/neovis22/ChromeRemoteObject

Examples:

Code: Select all

browser := new Chrome(a_scriptDir "\ChromeProfile")
page := browser.getPage()

Code: Select all

doc := page.eval("document")
doc.querySelector("body").innerHTML := "
(ltrim
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ul>
)"

Code: Select all

doc.querySelectorAll("li")[0].innerHTML := "<a href='https://autohotkey.com'>AutoHotkey</a>"
doc.querySelectorAll("li")[3].innerHTML := "<a href='https://google.com'>Google</a>"

for i, elem in (doc.querySelectorAll("a"), text := "")
    text .= elem.innerText " (" elem.getAttribute("href") ")`n"
MsgBox % text

Code: Select all

func := page.eval("(function(a, b) { return a + b })")
MsgBox % func.call("", 3, 5)

Code: Select all

arr := page.eval("['hello', 'world']")
for i, v in (arr, text := "")
    text .= i ": " v "`n"
MsgBox % text

Code: Select all

js := page.eval("window")
js.myVar := "ahk is awsome"
MsgBox % page.eval("myVar")

Gh0sTG0
Posts: 55
Joined: 25 Jun 2018, 07:58

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

Post by Gh0sTG0 » 28 Sep 2022, 15:23

Hi. Hope someone can help me with Target.activateTarget, It activates chrome window if it's inactive:

Code: Select all

for vKey, oPage in Chrome.GetPageList()
	if oPage.type = "page"
	{
		PageInst := Chrome.GetPageBy("id", oPage.id)
		Sleep, 10000 ;I set some really big sleeps, so I'm sure that problem is here:
		
		PageInst.Call("Target.activateTarget", {targetId: oPage.id}) ;here chrome window suddenly activates.
		
		Sleep, 10000
		; here's some code
		
		PageInst.Disconnect()
	}
Any way to fix it and to activatetab witihout activating window?

Or, maybe, there's way to just ctrl+tab through tabs without activating?

PS Win11 oct22 update, Version 1.1.34.03 unicode 64bit

tester
Posts: 84
Joined: 10 Jun 2021, 23:03

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

Post by tester » 26 Oct 2022, 05:15

Hello, is there a way to get the HTTP status code of a currently loaded page? I'd like to stop the script for a while once the page gives 404.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

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

Post by malcev » 26 Oct 2022, 05:48

Add event listener to Network.responseReceived.

tester
Posts: 84
Joined: 10 Jun 2021, 23:03

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

Post by tester » 26 Oct 2022, 07:55

@malcev Thanks for the hint. Just an example would be very much appreciated.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

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

Post by malcev » 26 Oct 2022, 10:34

Read this topic, there were examples how to set callback to event.

Gh0sTG0
Posts: 55
Joined: 25 Jun 2018, 07:58

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

Post by Gh0sTG0 » 13 Nov 2022, 06:22

Hi. Is there some more effective way to check if all tabs loaded then this one:

Code: Select all

for vKey, oPage in Chrome.GetPageList()
	if oPage.type = "page"
	{
		PageInst := Chrome.GetPageBy("id", oPage.id)
		Try
			PageInst.WaitForLoad()
		PageInst.Disconnect()
	}
?
This one takes something from second to two seconds to check one page.

edhalsim
Posts: 9
Joined: 18 Nov 2022, 11:34

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

Post by edhalsim » 18 Nov 2022, 11:40

Hi,
I just started trying to work with this. I need to be able to connect to google.com and login. When I run the basic script from the readme, I get a Javascript SecurityError.
It says to run Chrome in debug mode (I assume: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222), but then when I run the test
script again it opens a new browser.

Can someone please help, or am I just not able to use this approach? If this isn't the best tool is there another one that would be better? I tried Selenium but it's a no-go.

Thanks.

Gh0sTG0
Posts: 55
Joined: 25 Jun 2018, 07:58

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

Post by Gh0sTG0 » 18 Nov 2022, 12:15

edhalsim wrote:
18 Nov 2022, 11:40
It says to run Chrome in debug mode (I assume: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222), but then when I run the test script again it opens a new browser.
Hi. On basics - in that test script there's some part like this:

Code: Select all

FileCreateDir, ChromeProfile
ChromeInst := new Chrome("ChromeProfile")
This part is creating the new profile of chrome in folder "ChromeProfile" (it should be created near you .ahk file).

If you want to use chrome.ahk with already created instance of chrome, you should start your script with:
1) you already have chrome launched with debugging port;
2) in your script you have (for example I'm writing mini-script that works on F1 button press):

Code: Select all

#Include Chrome.ahk

F1::
{
	PageInst := Chrome.GetPage(1) ; I'm just connecting to already existing chrome, to latest active tab (that's tab that if opened now).
	PageInst.Call("Page.navigate", {"url": "https://autohotkey.com/"}) ; This opens ahk site in tab.
	PageInst.WaitForLoad() ; this is waiting untill page will finish loading.
	PageInst.Disconnect() ; Don't forget to disconnect from tab when work in that tab is done.
	Return
}
And after this script work I have ahk main page loaded in my active tab.

:!: :!: :!: ALERT!!! :!: :!: :!:
It would be a really bad idea to use --remote-debugging-port=9222 on your main chrome profile with google account that your phone is connected to, that has your credit cards info, passwords, etc.
Better to use separate chrome profile for ahk things, maybe without google account at all.

For example, I have 2 desktop shortcuts for chrome:
1) "C:\Program Files\Google\Chrome\Application\chrome.exe"
2) "C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --user-data-dir="D:\ahk_chrome_profile"
First is for regular chrome, chrome.ahk has no access for it. Second is for ahk things.

Or you can use chrome (without google account) for ahk and another browser like opera or firefox, or etc, for your regular things like work, online shopping, visiting bank sites, etc.

edhalsim
Posts: 9
Joined: 18 Nov 2022, 11:34

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

Post by edhalsim » 18 Nov 2022, 15:44

Gh0sTG0 wrote:
18 Nov 2022, 12:15
If you want to use chrome.ahk with already created instance of chrome, you should start your script with:
1) you already have chrome launched with debugging port;
2) in your script you have (for example I'm writing mini-script that works on F1 button press):

Code: Select all

#Include Chrome.ahk

F1::
{
	PageInst := Chrome.GetPage(1) ; I'm just connecting to already existing chrome, to latest active tab (that's tab that if opened now).
	PageInst.Call("Page.navigate", {"url": "https://autohotkey.com/"}) ; This opens ahk site in tab.
	PageInst.WaitForLoad() ; this is waiting untill page will finish loading.
	PageInst.Disconnect() ; Don't forget to disconnect from tab when work in that tab is done.
	Return
}
I really appreciate the help. Here's what I tried:
1) Closed all instances of AutoHotkey and Chrome.
2) Launched Chrome on debugging port: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
3) Selected the Guest profile.
4) Executed your script above from command line: .\test.ahk The only thing I changed was the hotkey (!F1::)
5) Went to the Chrome instance and pressed Alt-F1.
Nothing happened. :headwall:


[Mod edit: Fixed quote tags which made the reply look like part of the quoted post.]

Post Reply

Return to “Scripts and Functions (v1)”