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

Post your working scripts, libraries and tools for AHK v1.1 and older
Gh0sTG0
Posts: 55
Joined: 25 Jun 2018, 07:58

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

Post by Gh0sTG0 » 19 Nov 2022, 02:27

edhalsim wrote:
18 Nov 2022, 15:44
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:
I'm not sure about (3), never used guest profile. Try to drop it for now, just start chrome with port.

Try to check that your chrome is really closed on (1) in Ctrl+Shift+Esc -> more and there's on processes tab there's no chrome.exe.
Try to change chrome://settings/system : turn off 1st settings, about "don't turn off working extensions", or smth like that.

Try to create a desktop shortcut with this inside:

Code: Select all

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
And start chrome from this shortcut.

Try to check your chrome.ahk file that should be in one folder with your .ahk script, there you should find:

Code: Select all

static DebugPort := 9222
Somewhere in the beginning of file, like line number 6-8. If it's not 9222 - your debugging-port number should be that one that is in chrome.ahk.

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

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

Post by jekko1976 » 25 Nov 2022, 02:46

Hello,
One of the biggest advantage of Chrome.ahk is that it can run in background while you are doing other with the pc.
So when I work in other programs it navigate and click and write and read in Chrome.
BUT when I have to download a file I can click and when I arrive at the SAVE AS windows, I don't know how to let this part run in background.

So I have to drive autohotkey in the "old school mode" sending click and data to the controls of the window.
I would like to ask if there is a way to run in background this part too, sending the name of the file and confirming the operation.
Thank you so much

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

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

Post by Gh0sTG0 » 25 Nov 2022, 02:49

jekko1976 wrote:
25 Nov 2022, 02:46
Hello,
One of the biggest advantage of Chrome.ahk is that it can run in background while you are doing other with the pc.
So when I work in other programs it navigate and click and write and read in Chrome.
BUT when I have to download a file I can click and when I arrive at the SAVE AS windows, I don't know how to let this part run in background.

So I have to drive autohotkey in the "old school mode" sending click and data to the controls of the window.
I would like to ask if there is a way to run in background this part too, sending the name of the file and confirming the operation.
Thank you so much
Hi. That saveas is part of system, not chrome or chrome.ahk. Maybe it will work for you if you set chrome settings to download all files in one folder, and then moving files where you want to.

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

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

Post by edhalsim » 01 Dec 2022, 14:48

Just started playing with this. I have a very simple script:

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://..."}) ; This opens ahk site in tab.
	PageInst.WaitForLoad() ; this is waiting until page finishes loading.
	Sleep, 1000

	PageInst.Evaluate("document.querySelector('input[type=button]').click();")
	PageInst.WaitForLoad()
	Return
}
I'm just trying to click a button on the page. I'm getting: Cannot read properties of undefined (reading 'click')
I know the query selector works because I can run it in Chrome's console and it works fine. What am I doing wrong?

thaihoa3189
Posts: 35
Joined: 23 Mar 2022, 22:10

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

Post by thaihoa3189 » 09 Dec 2022, 15:09

20170201225639 wrote:
18 Jan 2021, 05:21
I've been running Chrome in debug mode for almost a month now and I don't see any performance-related reasons why I shouldn't just run debug mode all the time, since it feels just as fast as non-debug mode, and the productivity gains from Chrome.ahk is simply too great.

If you'd like to run Chrome in debug mode always, in addition to making a custom shortcut, you can also change the registry, so that even when Chrome is launched by other apps or via double-clicking an .html file, it's still launched in debug mode.

Code: Select all

Value = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 -- "`%1"
RegWrite, REG_SZ, HKEY_CLASSES_ROOT, ChromeHTML\shell\open\command, , %Value%
Source: https://stackoverflow.com/questions/51563287/how-to-make-chrome-always-launch-with-remote-debugging-port-flag
I changed the registry like you said but when i open Chrome, it doesnt work. Is something wrong?

lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

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

Post by lexikos » 09 Dec 2022, 17:51

@thaihoa3189
I have no interest in the topic and don't even have Chrome installed, but if I were to hazard a guess, it probably has to do with which method you are using to launch Chrome. That registry setting will only affect Chrome being launched via file type (.html) or protocol (http) associations which are linked with the ChromeHTML key. If you use a shortcut, you need to edit the shortcut.
in addition to making a custom shortcut

thaihoa3189
Posts: 35
Joined: 23 Mar 2022, 22:10

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

Post by thaihoa3189 » 09 Dec 2022, 21:35

@lexikos
Thank u for explaning it. I understand now, i will try another ways.

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

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

Post by edhalsim » 12 Dec 2022, 15:58

I hope this isn't a stupid question. Is there any way to get the contents of an iFrame whose domain is different? I know cross-domain scripting may prevent it, but I need to check the attributes of an element inside the iframe. If Autohotkey doesn't support it, can you think of any other scripting solutions for Chrome that may allow it?

Thanks.

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 » 13 Dec 2022, 00:01

edhalsim wrote:
12 Dec 2022, 15:58
I hope this isn't a stupid question. Is there any way to get the contents of an iFrame whose domain is different? I know cross-domain scripting may prevent it, but I need to check the attributes of an element inside the iframe. If Autohotkey doesn't support it, can you think of any other scripting solutions for Chrome that may allow it?

Thanks.
You need to use command line switches "--disable-site-isolation-trials" and "--disable-web-security" that allows you cross-origin access.
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

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

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

Post by edhalsim » 13 Dec 2022, 17:13

You need to use command line switches "--disable-site-isolation-trials" and "--disable-web-security" that allows you cross-origin access.
Here's what I tried.
I updated my shortcut to: "C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --disable-site-isolation-trials --disable-web-security
I added this to my code:

Code: Select all

iFrameJS = var iFrame = document.getElementById('tabs-2').querySelector('div[class="class"] iframe'); iFrame.contentWindow.document.getElementById('elemId')
PageInst.Evaluate(iFrameJS)
It gave me the error: DOMException: Blocked a frame with origin 'https://...' from accessing a cross-origin frame.

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 » 14 Dec 2022, 00:55

edhalsim wrote:
13 Dec 2022, 17:13
You need to use command line switches "--disable-site-isolation-trials" and "--disable-web-security" that allows you cross-origin access.
Here's what I tried.
I updated my shortcut to: "C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --disable-site-isolation-trials --disable-web-security
I added this to my code:

Code: Select all

iFrameJS = var iFrame = document.getElementById('tabs-2').querySelector('div[class="class"] iframe'); iFrame.contentWindow.document.getElementById('elemId')
PageInst.Evaluate(iFrameJS)
It gave me the error: DOMException: Blocked a frame with origin 'https://...' from accessing a cross-origin frame.
you can try this one of them at once "--disable-auto-wpt-origin-isolation" or "-disable-site-isolation-for-policy", one of them will launch chrome with a warning bar saying "stability and security will suffer" that will made me access isolated frames long ago.
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

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

Post by Sabestian Caine » 24 Dec 2022, 13:22

I simply want to get/extract the link (href) by using Chrome.ahk so can anyone tell me how to code?
for example I want to get/extract the link (href) as shown in red circle in the below image-
24_12_22 @11_45_06.PNG
24_12_22 @11_45_06.PNG (90.75 KiB) Viewed 3936 times
my codes are these-

Code: Select all

#NoEnv
SetBatchLines, -1

#Include  D:\tests\lib\Chrome\Chrome.ahk ;Remember to put Chrome in your library folder
page:=Chrome.GetPageByURL("https://www.autohotkey.com/boards/viewforum.php?f=76") ;This will connect to the second index of a specific tab
If !IsObject(page){
MsgBox % "That wasn' t object / the page wasn't found"
ExitApp
}

MsgBox % Chrome.Jxon_Dump(page.Evaluate(WHAT CODE HERE WILL COME....???))

I tried to learn things from this video-
https://www.youtube.com/watch?v=2T8oWlf5r7w&ab_channel=Joe-AUTOHOTKEYGuru

But it seems that one should have deep knowledge of JavaScript for doing that kind of stuff after watching this video.
Can anyone tell me next line of code to get the link/href of that thing...???
Thanks a lot...
I don't normally code as I don't code normally.

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

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

Post by Gh0sTG0 » 24 Dec 2022, 13:39

Hi, @Sabestian Caine
Not sure about "jxon_dump", never used it, I'm just using this configuration:

Code: Select all

my_result := Page.Evaluate("document.getElementsByClassName('forumtitle')[1].href;").Value
Try to just take part that is in (), maybe it will work
Part inside " you can take from chrome console. Like press F12 and look there.
On elements tab you can find correct thing (Press Ctrl+Shift+C and then left click on element you are interested in. It will be selected in Elements tab). Then on console tab you can check and build your code.
If you write in console:

Code: Select all

document.getElementsByClassName('forumtitle')[1]
It will highlight on the page, so you can try to change 1 to 2, 3, 4, etc. First one has id "0".
There are some more ways of getting things, like you can search by ID, by Name, by ClassName, by TagNameNS.
Don't forget about .Value in the end.
And in the end of file, use:

Code: Select all

Page.Disconnect()
Or if you will reconnecting too much times, your chrome will start refusing new connects.

My full code for your target will be:

Code: Select all

#Include  Chrome.ahk

F1::
{
	Page := Chrome.GetPageByURL("https://www.autohotkey.com/boards/viewforum.php?f=76") ; I'm thinking that there IS that tab there, so here without checking. Ofc you can use your one. Or wrap your getpage into Try...Catch... thing.
	; Page := Chrome.GetPage(1) ; Or you can just connect to the now-is-active tab. This should be just the tab you see in front.
	my_result := Page.Evaluate("document.getElementsByClassName('forumtitle')[1].href;").Value
	MsgBox, %my_result%
	Page.Disconnect() ; Don't forget to disconnect. There's some really small limit of number of connections Chrome can have not-disconnected.
	Return
}
And for me it MsgBox'es this:

Code: Select all

https://www.autohotkey.com/boards/viewforum.php?f=82
Hope that helps you in understanding how things are working.

User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

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

Post by Sabestian Caine » 24 Dec 2022, 13:58

@Gh0sTG0

Thanks a lot Gh0sTG0.....you really solved it in very simple way....I am wondering that if there could be so simpler solution of this then why there is so lengthy and confusing process is shown in this video- https://www.youtube.com/watch?v=2T8oWlf5r7w&ab_channel=Joe-AUTOHOTKEYGuru
earlier I used to automate chrome by using selenium and in selenium i used to use this kind of syntax - getElementsByClassName/FindelementbyID etc. anyway you did a great job Sir....Thank you so much once again :clap:
I don't normally code as I don't code normally.

User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

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

Post by Sabestian Caine » 25 Dec 2022, 13:04

@Gh0sTG0

Dear Gh0sTg0.... i wish to know one more thing... That, suppose i want to click on the same link as shown in this image-
25_12_22 @11_30_48.PNG
25_12_22 @11_30_48.PNG (23.09 KiB) Viewed 3716 times

Then i am using these codes-

Code: Select all

#Include  D:\tests\lib\Chrome\Chrome.ahk ;Remember to put Chrome in your library folder

page:= Chrome.GetPageByUrl("https://www.autohotkey.com/boards/viewforum.php?f=76")
page.Evaluate("document.querySelector('#page-body > div.forabg > div > ul.topiclist.forums > li:nth-child(2) > dl > dt > div > a').click()")
However the above codes are working fine but i want to know that- can there be any simple approach to do that?
Please tell me...Thanks a lot...
I don't normally code as I don't code normally.

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

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

Post by Gh0sTG0 » 25 Dec 2022, 13:10

@Sabestian Caine
It's bit easier.

Code: Select all

Page.Evaluate("document.getElementsByClassName('forumtitle')[1].click();")
Page.WaitForLoad()

User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

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

Post by Sabestian Caine » 27 Dec 2022, 12:50

@Gh0sTG0

Dear Gh0sTG0... Could you please help me little more...
As suppose I want to extract text from this site-
https://www.softr.io/blog/tools-to-extract-data-from-website

I want to extract the text shown in the red box in the following image-
27_12_22 @11_16_32.PNG
27_12_22 @11_16_32.PNG (95.45 KiB) Viewed 3558 times
When, I do inspect elements to know its class or id etc then it shows like this-
Untitled.png
Untitled.png (75.97 KiB) Viewed 3558 times
Could you please tell me how can i extract that text using Chrome.ahk....???
i am not finding any way to do that...
please help...
Thanks a lot...
I don't normally code as I don't code normally.

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

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

Post by Gh0sTG0 » 27 Dec 2022, 13:56

@Sabestian Caine
That's not literally chrome.ahk things, but, thing inside Evaluate("") should be like:

Code: Select all

document.getElementsByClassName('ql-editor')[0].children[0].innerText
Algorythm:
1) Find thing with classname or id that is higher level that thing you want to get, check that you get contains thing you need, like here it is that div with classname "ql-editor";
2) Go lower level with children[number] as much times as needed;
3) Get innerText, or innerHTML, of href, or what ever you want to get.

User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

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

Post by Sabestian Caine » 28 Dec 2022, 12:28

@Gh0sTG0


Thanks dear Gh0sTG0....you are really awesome....!!!! :thumbup:
I don't normally code as I don't code normally.

User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

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

Post by Sabestian Caine » 30 Dec 2022, 08:03

@Gh0sTG0

Dear Gh0sTG0... I am trying to unfriend many friends from my facebook account (as it has gone to thousands of friends) and for this i am trying to use these codes-

Code: Select all

#Include  D:\tests\lib\Chrome\Chrome.ahk ;Remember to put Chrome in your library folder
page:= Chrome.GetPageByUrl("facebook", "contains")
Page.Evaluate("document.getElementsByClassName('x1ey2m1c xds687c xg01cxk x47corl x10l6tqk x17qophe x13vifvy x1ebt8du x19991ni x1dhq9h x1wpzbip x14yjl9h xudhj91 x18nykt9 xww2gxu')[4].click();")

Sleep 500

Page.Evaluate("document.getElementsByClassName('x1i10hfl xjbqb8w x6umtig x1b1mbwd xaqea5y xav7gou xe8uvvx x1hl2dhg xggy1nq x1o1ewxj x3x9cwd x1e5q0jg x13rtm0m x87ps6o x1lku1pv x1a2a7pz xjyslct x9f619 x1ypdohk x78zum5 x1q0g3np x2lah0s x13mpval x1w4qvff xdj266r xat24cr x1n2onr6 x16tdsg8 x1ja2u2z x6s0dn4 x1y1aw1k x1sxyh0 xwib8y2 xurb0ha')[3].click();")

these codes first clicks on this button-
30_12_22 @6_29_31.PNG
30_12_22 @6_29_31.PNG (127.96 KiB) Viewed 3345 times

and then it clicks on this button-

30_12_22 @6_31_57.PNG
30_12_22 @6_31_57.PNG (111.44 KiB) Viewed 3345 times

but these codes are not working properly. these codes work 2-3 times fine and they they start showing error. Could you please tell me any better solution for this...??? please help me once more sir...
THANKS A LOT...
I don't normally code as I don't code normally.

Post Reply

Return to “Scripts and Functions (v1)”