Chrome library for ahk v2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
ositoMalvado
Posts: 182
Joined: 24 Dec 2019, 12:02
Contact:

Chrome library for ahk v2

Post by ositoMalvado » 31 Mar 2023, 18:04

Hi guys, does exist CHROME AHK v1.3 for AHKV2?

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

I use this for v1, but cant find for v2, someone please, thanks!!
My WEB some useful stuff

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Chrome library for ahk v2

Post by swagfag » 31 Mar 2023, 22:24

well there used to be one for v2 some days ago if memory serves right but geekdude must have deleted the branch for some reason

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Chrome library for ahk v2

Post by teadrinker » 01 Apr 2023, 15:50

There is this one, not tested yet.

jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: Chrome library for ahk v2

Post by jsong55 » 06 Apr 2023, 20:08

I tried it, for some reason it doesn't work get lots of errors.

Code: Select all

#Requires AutoHotkey v2.0-a
#SingleInstance Force
#Include <chrome>

chro:=chrome()
page:=chro.GetPage()
url:=page.Evaluate("window.location.href").value
msgbox url
In particular this error

Code: Select all

Error: This value of type "String" has no method named "Evaluate".

	005: chro := chrome()
	006: page := chro.GetPage()
▶	007: url := page.Evaluate("window.location.href").value

jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: Chrome library for ahk v2

Post by jsong55 » 07 Apr 2023, 03:50

Ok to answer my question and having fixed it. There is a difference in that now page.evaluate(JS) is a type "Map" hence difference syntax to call the value.
Also, we need JSON.ahk and WebSocket.ahk from
https://github.com/thqby/ahk2_lib

so this final code works for me

Code: Select all

#Requires AutoHotkey v2.0-a
#SingleInstance Force
#Include <Chrome>
#Include <JSON>
xCh:=Chrome()
page:=xCh.GetPage()
url:=page.evaluate("window.location.href")["value"]
msgbox url
if you try this to do it on this page viewforum.php?f=82

Code: Select all

JS:="document.querySelectorAll('dt > div.list-inner > a.forumtitle')[0].innerText"
msgbox page.Evaluate(JS)["value"]
You should get "Gaming"

Rakkar
Posts: 7
Joined: 29 Jun 2023, 20:52

Re: Chrome library for ahk v2

Post by Rakkar » 07 Aug 2023, 21:47

Hi all, I'm struggling to make chrome.ahk work, as so much of the advice is for v1, and I can get a bit stuck translating it into v2. I'm trying to open a new window that chrome.ahk can work with, open a webpage in a new tab, wait for it to load, click a button, and wait for the new page to load. In this example, I've used the sign-in button on google.com

however, WaitForLoad() doesn't seem to work, the script rushes through to the end, and the evaluate/click command is delivered to a still-loading webpage, and so isn't being done. The script works if I replace WaitForLoad() with a sleep command, but I don't want a script that relies on a webpage loading within a guessed duration. It's probably because I don't understand classes well enough, but any advice is much appreciated.

Code: Select all

myURL := "https://www.google.com/"
xCh := Chrome()

Run('"C:\Program Files\Google\Chrome\Application\chrome.exe " --remote-debugging-port=9222 --remote-allow-origins=*  --user-data-dir="C:\remote-profile"')
winwait "- Google Chrome"
WinActivate

myPage := xCh.NewPage(myURL)
myPage.WaitForLoad()

myPage.evaluate("document.querySelector('#gb > div > div.gb_0d > a > span').click()")
myPage.WaitForLoad()

;do all the other things I want to do.

emp00
Posts: 147
Joined: 15 Apr 2023, 12:02

Re: Chrome library for ahk v2

Post by emp00 » 08 Aug 2023, 09:54

Alternative recommendation: https://github.com/Descolada/UIA-v2

Additionally there is UIA_Browser.ahk, which contains helper functions for browser automation (fetching URLs, switching tabs etc). Currently it supports Chrome, Edge, and Firefox.

Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

Re: Chrome library for ahk v2

Post by Descolada » 08 Aug 2023, 10:10

emp00 wrote:
08 Aug 2023, 09:54
Alternative recommendation: https://github.com/Descolada/UIA-v2

Additionally there is UIA_Browser.ahk, which contains helper functions for browser automation (fetching URLs, switching tabs etc). Currently it supports Chrome, Edge, and Firefox.
Please note that UIA is a much worse option than Chrome.ahk or Rufaydium, and I recommend it only when those options are not available. It's slower, less reliable, dependent on the browser layout, language, user options etc.

User avatar
Datapoint
Posts: 295
Joined: 18 Mar 2018, 17:06

Re: Chrome library for ahk v2

Post by Datapoint » 08 Aug 2023, 22:06

@Rakkar
.WaitForLoad() seems to work for me. If it doesn't work for you then you could try looping until a certain element on the page is present.

I posted a V2 example in the Chrome.ahk thread that does something similar to what you are trying to do. (link)

This worked for me:

Code: Select all

#Requires AutoHotkey v2
#SingleInstance Force
#Include Chrome.ahk
#Include JSON.ahk

myURL := "https://www.google.com/"
ChromeProfilePath := A_AppData "\Google\Chrome\MyProfile"
ChromeInstance := Chrome(,,,, ChromeProfilePath)
PageInstance := ChromeInstance.NewPage()
PageInstance.Call("Page.navigate", {url: myURL})
;PageInstance.WaitForLoad()
JS := "document.querySelector('#gb > div > div.gb_Md > a > span')"
; wait for the button to be present
;for k, v in PageInstance.Evaluate(JS) ; Evaluate returns a map.
;	MsgBox ">" k "`n>" v
while PageInstance.Evaluate(JS)["subtype"] = "null"
	Sleep 100
JS := "document.querySelector('#gb > div > div.gb_Md > a > span').click()"
PageInstance.Evaluate(JS)
ExitApp

; Chrome.ahk for v2 https://github.com/thqby/ahk2_lib/blob/master/Chrome.ahk
; JSON.ahk for v2 https://github.com/thqby/ahk2_lib/blob/master/JSON.ahk

Post Reply

Return to “Ask for Help (v2)”