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

Post your working scripts, libraries and tools
Newer2AHK
Posts: 59
Joined: 24 Jun 2020, 10:32

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

02 Aug 2020, 08:17

Ah, that makes sense. So, "%value%" was probably just a typographical error, intended to be "%val%". I've now made that correction in my copy of Chrome.ahk. Thank you.
dekinhow
Posts: 7
Joined: 21 Aug 2017, 22:31

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

03 Aug 2020, 14:33

So yesterday I had a windows update and now my script is not working anymore.

I tracked down to be Chrome.ahk, because even setting up the basic example on github would give me this error:

Image here: https://i.ibb.co/TKbhqWK/Capture.png

Any ideas how to fix it?
gregster
Posts: 5211
Joined: 30 Sep 2013, 06:48

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

03 Aug 2020, 14:36

dekinhow wrote:
03 Aug 2020, 14:33
Any ideas how to fix it?
Could be an issue with anti-virus or firewall...
dekinhow
Posts: 7
Joined: 21 Aug 2017, 22:31

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

03 Aug 2020, 14:43

gregster wrote:
03 Aug 2020, 14:36
dekinhow wrote:
03 Aug 2020, 14:33
Any ideas how to fix it?
Could be an issue with anti-virus or firewall...
I only use Windows default defender settings... I just turned them all off and the issue persists =/
Balovauto
Posts: 4
Joined: 17 Apr 2020, 03:22

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

07 Aug 2020, 02:49

to geekdude, grepster and anyone:

Do you guy know how to make autohotkey work with Opera browser? Thanks
burque505
Posts: 1378
Joined: 22 Jan 2017, 19:37

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

07 Aug 2020, 10:22

@dekinhow, I occasionally see exactly the same error on both Win10 and Win7. It usually means I have an orphaned instance of chrome.exe, chromedriver.exe or both still running. It has been happening a little more often since Chrome updated to version 84.
Regards,
burque505
burque505
Posts: 1378
Joined: 22 Jan 2017, 19:37

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

07 Aug 2020, 10:27

@Balovauto, try the operachromiumdriver. If it doesn't work out-of-the-box with Chrome.ahk, perhaps adapting @GeekDude's script for Firefox portable (link) will work.
Regards,
burque505
gregster
Posts: 5211
Joined: 30 Sep 2013, 06:48

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

07 Aug 2020, 11:03

Balovauto wrote:
07 Aug 2020, 02:49
to geekdude, grepster and anyone:

Do you guy know how to make autohotkey work with Opera browser? Thanks
Afaics, the fastest (and easiest) way to re-use Chrome.ahk for other Chromium-based browsers which support the same debug protocol (which includes Opera and the new Edge) would be to just use the path to the browsers's exe as the fourth parameter for Chrome.ahk's new() contructor:

Code: Select all

operaPath := "C:\Users\g\AppData\Local\Programs\Opera\70.0.3728.95\opera.exe"
ChromeInst := new Chrome("OperaProfile", , , operaPath)
Note that the path might vary on your computer and will change, if Opera updates to a new version.

But it seems, for Opera, you can also use the more general

Code: Select all

operaPath := "C:\Users\g\AppData\Local\Programs\Opera\launcher.exe"
That means, you can just do something like this, and simply use Chrome.ahk like you would with Chrome (after creating an Opera instance):

Code: Select all

#NoEnv
SetBatchLines, -1
SetTitleMatchMode 2
#Include Chrome.ahk											; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=42890

url := "https://autohotkey.com"    	; url := "https://google.com"
; operaPath := "C:\Users\g\AppData\Local\Programs\Opera\70.0.3728.95\opera.exe"
operaPath := "C:\Users\g\AppData\Local\Programs\Opera\launcher.exe"

; --- Create a new Opera instance ---
FileCreateDir, OperaProfile										; creates a profile subdirectory in current folder
ChromeInst := new Chrome("OperaProfile", , , operaPath)
winwait, – Opera

; --- Connect to the page ---
if !(Page := ChromeInst.GetPage(  ))		 
{
	MsgBox, Could not retrieve page!
	ChromeInst.Kill()
}
else
	Page.WaitForLoad()

Page.Call("Page.navigate", {"url": url})			; Navigate to url
Page.WaitForLoad()
return

Esc::ExitApp
(works here; afaik, the same works for the Edge browser.)

Please note that this only works, if you plan to start a new Opera debug instance. To detect already running Opera instances (in debug mode) and to connect to them, you would also need to change the hard-coded references in the Findinstances() method in Chrome.ahk (then you could also change the Chrome references in the New() method).

I guess you could either change it in the original class (make a copy) - or create an Opera class which could extend the Chrome class and overwrite the methods that need to be replaced... the rest of the code can simply be re-used. So, only minor changes would be needed :)
burque505
Posts: 1378
Joined: 22 Jan 2017, 19:37

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

07 Aug 2020, 11:54

Thanks, @gregster. Great info.
Regards,
burque505
pmobin
Posts: 4
Joined: 14 Apr 2016, 12:50

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

15 Aug 2020, 15:20

Hello all,

My question is so basic that I am a bit embarassed : )
I downloaded the chrome.ahk library and figured out how to also add the two empty subfolders under lib which I am sure there is a better way.
Upon running the include line of the chrome.ahk I get an error on the very line 1 that Class Chrome is unrecognized.

Specifically in 1.2
Error at line 5 in #include file.... chrome.ahk

Line Text: class Chrome
Error: This line does not contain a recognized action.

The program will exit.
I am sure it is something simple/dumb.
Appreciate getting me going
gregster
Posts: 5211
Joined: 30 Sep 2013, 06:48

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

15 Aug 2020, 15:24

pmobin wrote:
15 Aug 2020, 15:20
Upon running the include line of the chrome.ahk I get an error on the very line 1 that Class Chrome is unrecognized.

Specifically in 1.2
Error at line 5 in #include file.... chrome.ahk

Line Text: class Chrome
Error: This line does not contain a recognized action.
Are you running some ancient 1.0.x version of AHK?
figured out how to also add the two empty subfolders under lib which I am sure there is a better way
Not sure, what this means... are you talking about AHK's local, user or standard library? But which two empty subfolders?
At least for testing, I would put Chrome.ahk directly into the script's directory...
pmobin
Posts: 4
Joined: 14 Apr 2016, 12:50

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

15 Aug 2020, 15:48

Thanks @gregster I was running a 2009 version and have no idea why. I am usually good about upgrading.
GeekDude
Posts: 886
Joined: 02 Oct 2013, 22:13

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

16 Aug 2020, 14:13

@pmobin

You get empty/broken files when you download the partial source used to build the library instead of the library itself. See this post from page 1 of the thread:
GeekDude wrote:
17 Jan 2018, 07:39
Make sure you're downloading the release file "Chrome.ahk_v1.0.zip" from the releases page, not the source code. As you have learned, GitHub does not include submodules (i.e. AutoHotkey-JSON and WebSocket.ahk) in its source downloads. The release has all the #Include's built in (no lib folder needed :) ).

Image
neogna2
Posts: 74
Joined: 15 Sep 2016, 15:44

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

17 Aug 2020, 11:10

Worth mentioning again that the latest release on https://github.com/G33kDude/Chrome.ahk/releases is Chrome.ahk_v1.2.zip and its file Chrome.ahk has a typo. Search for App Pahs\chrome.exe and replace with App Paths\chrome.exe
User avatar
Previz
Posts: 24
Joined: 01 May 2020, 19:28

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

01 Sep 2020, 12:09

Hello guys, I am looking for some clarification before I commit myself in learning to use Chrome.ahk. Specifically for Firefox

I get the impression most people use chrome.ahk to do some web scrapping here and there and then disconnect it and set it aside until next time.

But I am looking to use it as a means of getting AHK to control web “apps” or pages programmatically, including address bar.
For example, to configure AHK hotkeys to toggle the visibility of an element on a website, set the text of a textbox or send a URL to the currents tabs address bar

This would mean a constant connection between Chrome.ahk and FF, a “port” open and having FF running in debug mode.

Is Chrome.ahk meant to be used this way? Can you establish a connection through a port once and just forget about it? (of course, not including computer restarting and sleeping ) Thanks!
Tre4shunter
Posts: 72
Joined: 26 Jan 2016, 16:05

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

03 Sep 2020, 16:47

Probably more trouble than its worth but...

I wanted to be able to launch the same chrome page(a local html file) multiple times - without having multiple debug ports open, but yet make sure than each page instance could be interacted with independently. If you arent careful - you will end up connecting to a different instance of your app which is running on the same port.

I also wanted each page that opened to be in 'window' mode. Unfortunately, even in you start chrome with the --app="url" switch, any additional pages you open will NOT be in app mode, so you will show the navigation and url bar, etc. If anyone knows a workaround for this using the devtools protocol, im all ears.

Here is my solution:

First - i modified the _new Constructor slightly in the chrome class to ensure the first Chrome Instance i created was available before i try to connect to the websocketdebugger URL(It would sometimes try to get the pageinstance before it was available on the 127.0.0.1:port:/json endpoint):

Code: Select all

		DetectHiddenWindows, On
		start := A_TickCount
		while !WinExist("ahk_pid" . OutputVarPID){
			tooltip % A_TickCount - Start
			if(A_TickCount - Start > 3000)
				return, -1
		}
		DetectHiddenWindows, % DHW
		
All the above does is wait for the PID to exist - if not in 3 seconds, returns -1

Works reliably for the *First* Chrome Instance. After Chrome instance created, i connect to the page via URL

Code: Select all

	PageInstance := ChromeInst.GetPageByURL("file:///" StrReplace(Path,"\","/"), "exact",,BoundCallBack)
For each subsequent time i open the same app - the above waiting for PID FAILS, because chrome does black magic with the PID it returns after the first instance is created. So, the solution for me was 2 steps:

1. I had to add a 1 second sleep after calling 'ChromeInst := new chrome(...)' - the class constructor would always return too early, and the subsquent GetPageBy calls would fail as the /json endpoint was not ready. Using Sleeps is never ideal - so im all ears to suggestions on this one.

2. To make sure create a temporary copy of my .html file with a new name each time so that you dont need to worry about attaching to the incorrect page:

Code: Select all

Path := A_ScriptDir "\Quote" A_TickCount ".html"
FileRead, htmldoc, % A_ScriptDir "\Quote.html"
FileAppend, % htmldoc, % Path
This way - You can launch several Chrome Instances in seperate scripts - using the same debug port.

I mucked around with this for days - if someone has a better way to launch multiple chrome pages - in APP mode - on the same debug port - please let me know!

Thanks for listening...
Tre4shunter
Posts: 72
Joined: 26 Jan 2016, 16:05

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

03 Sep 2020, 18:48

Example of what i mean -

3 copies of my app running on the same debug port all in 'app' mode:

https://imgur.com/a/2IU1wAQ
ars4l4n
Posts: 5
Joined: 26 Aug 2020, 07:27

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

12 Sep 2020, 16:03

So I was watching the Part 3 of Automating Chrome via the Chrome library video tutorial and the author says I should look out for an imported script, in this case ace.js to look at the documentation of that particular service and see how I can interact with that type of code.
Now I'm on this site with a huge source code (amazon.de) and I have no Idea what to look for.
Maybe in this section it's just java script? (it says javascript on top of the codesection I'm looking at (see pic) but I'm not sure where javascript has to stand to relate to that section)

Perhaps you can help me out when I say what my goal is:
I want to navigate to the search field of an amazon.de page automatically as soon as it loads.
I've used the element picker to find out which code portion it's from and it highlighted:

Code: Select all

<input type="text" id="twotabsearchtextbox" value="" name="field-keywords" autocomplete="off" placeholder="" class="nav-input" dir="auto" tabindex="0" aria-label="Search">
Then I toggled the device toolbar and discovered that the code portion above the highlighted code changed a word (highlighted in purple, see pic) as I clicked in and outside of the searchbar

Code: Select all

class="nav-searchbar"
to

Code: Select all

class "nav-searchbar nav-active"
How would I change that portion of the script with java via the chrome.ahk library?

Btw, how many posts do I need to post links without them getting broken by the messageboard?
Attachments
chrome_2020-09-12_22-31-30.png
chrome_2020-09-12_22-31-30.png (34.77 KiB) Viewed 183 times
Tre4shunter
Posts: 72
Joined: 26 Jan 2016, 16:05

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

12 Sep 2020, 18:43

Navigate to the field, and 'activate' it? or do you just want to input some text into it? because you dont need to worry about that classname if you just want to input some text id imagine.
burque505
Posts: 1378
Joined: 22 Jan 2017, 19:37

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

12 Sep 2020, 19:16

Hi @ars4l4n, maybe this will help. Seems to be working for me. Quick and dirty, sorry about that.

Code: Select all

#Include Chrome.ahk
ChromeInst := new Chrome("ChromeProfile")
PageInstance := ChromeInst.GetPage()
PageInstance.Call("Page.navigate", {"url": "https://www.amazon.de/"})
PageInstance.WaitForLoad()
Sleep, 1000
PageInstance.Evaluate("document.querySelector(""#twotabsearchtextbox"").value = ""Müller's Esel, Müller's Kuh, usw."";")
Sleep, 5000
PageInstance.Call("Browser.close")
ExitApp
Regards,
burque505

Return to “Scripts and Functions”

Who is online

Users browsing this forum: SirSocks and 12 guests