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

Post your working scripts, libraries and tools
fenchai
Posts: 184
Joined: 28 Mar 2016, 07:57

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

19 Mar 2018, 14:32

Shadowpheonix wrote:
fenchai wrote:Is there any reason everytime chrome opens, it shows something about windows defender wanting to reset my chrome preferences and adobe and google drive keep wanting to install? this is weird. :shock:


This sounds like a problem with either your Chrome profile or with an extension/plugin you have installed in Chrome.
If you haven't already done so, try running with a brand new profile in Chrome and see if the issue still occurs. I am not sure what else to suggest if that fails.


True, the problem was on my end. I tried the same on my working PC (without any profile) and it works just fine. So I always need to make a new profile for this to work? it doesn't seem very time saving if I have to do this every time. hahah I guess i will have to wait until we get it to work on other than empty profiles.
Shadowpheonix
Posts: 1258
Joined: 16 Apr 2015, 09:41

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

19 Mar 2018, 15:01

fenchai wrote:
Shadowpheonix wrote:
fenchai wrote:Is there any reason everytime chrome opens, it shows something about windows defender wanting to reset my chrome preferences and adobe and google drive keep wanting to install? this is weird. :shock:


This sounds like a problem with either your Chrome profile or with an extension/plugin you have installed in Chrome.
If you haven't already done so, try running with a brand new profile in Chrome and see if the issue still occurs. I am not sure what else to suggest if that fails.


True, the problem was on my end. I tried the same on my working PC (without any profile) and it works just fine. So I always need to make a new profile for this to work? it doesn't seem very time saving if I have to do this every time. hahah I guess i will have to wait until we get it to work on other than empty profiles.


A new profile should not be needed every time. I use the same profile every time on my system and Chrome gives me no issues.
If you are getting the same issue the second time you use a profile, then I suspect you are logging into your Google account and Chrome is having trouble syncing extensions, bookmarks, etcetera. Try making a new profile and disabling the sync on it. If that clears up the issue, enable one type of sync and try again - keep enabling sync types & relaunching with that profile until you find the one that triggers the issue.
User avatar
Joe Glines
Posts: 542
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

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

20 Mar 2018, 07:51

Shadowpheonix wrote:
Joe Glines wrote:this still leaves me clueless as to how to read an existing value from a web page in Chrome without having to install Selenium. :lol:


Sorry- I thought you were trying to set a value. I have something similar for getting values but am busy today preparing for our webinar on Neural Networks with AutoHotkey. I'll try and get to it later this week / this weekend.

Regards,
Joe
GeekDude
Posts: 827
Joined: 02 Oct 2013, 22:13

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

21 Mar 2018, 16:59

Chrome.ahk v1.1 has been released

Please see the original post for a link to the release and revision history pages.



Changes

  • class Chrome's constructor now accepts additional flags for chrome
  • Pages are no longer connected to using Chrome.GetTab()

Additions

  • New methods for finding the right page to connect to
  • New method Chrome.Kill() for ending the Chrome process
  • New example script ExportPDF.ahk demonstrating headless chrome and PDF exports.
Shadowpheonix
Posts: 1258
Joined: 16 Apr 2015, 09:41

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

22 Mar 2018, 13:15

GeekDude wrote:If you have a chrome instance already running in debug mode, you can skip the initialization of the Chrome class and just call Chrome.GetTab() directly. For example:

Code: [Select all] [Download] GeSHi © Codebox Plus

#Include Chrome.ahk

TabInst := Chrome.GetTab()
TabInst.Evaluate("alert('hi!');")


What is the replacement for this process with v1.1? GetTab() no longer exists in the class, and PageInst := Chrome.GetPage() returns a "connection with the server could not be established" error.
GeekDude
Posts: 827
Joined: 02 Oct 2013, 22:13

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

22 Mar 2018, 23:55

ramonstart wrote:Hello

Why does the Jxon_Dump method return the "menor preu00E7o do livro" value when the correct text is "menor preço do livro"?

I'm using the call below to bring the content of the list that is in the url https://www.estantevirtual.com.br/livros/robert-t-kiyosaki/pai-rico-pai-pobre/3770213412?q=Pai%20Rico%20Pai%20Pobre%20Robert%20T%20Kiyosaki

Would you help me?

I would have expected it to output \u00E7 with a backslash. The sequence \u marks the next four characters as a hexadecimal representation of a Unicode character's code point. Code point 00E7 corresponds to ç, which can be seen on Wikipedia's list of Unicode characters.

Image




Shadowpheonix wrote:
Joe Glines wrote:
Shadowpheonix wrote:In the "pastebin.ahk" example that GeekDude was nice enough to include in the release package, the Tab.Call("DOM.setAttributeValue", {"nodeId": NameNode.NodeId, "name": "value", "value": "ChromeBot"}) line assigns a value to an input field. How would I go about retrieving the value after it was assigned?


I don't have an example I can post right now but I "solved this" by inserting Java script.



If you happen to come up with an example you can post, it would be most appreciated. :D

Something like MsgBox, % PageInst.Evaluate("document.querySelector('input[name=desc]').value").value should work.




fenchai wrote:Is there any reason everytime chrome opens, it shows something about windows defender wanting to reset my chrome preferences and adobe and google drive keep wanting to install? this is weird. :shock:

You may be able to disable those warnings by providing the --no-first-run and --disable-extensions flags in the Flags parameter of new Chrome(ProfilePath, URL, Flags) in Chrome.ahk_v1.1. For example, ChromeInst := new Chrome("ChromeProfile",, "--no-first-run --disable-extensions")




Shadowpheonix wrote:
GeekDude wrote:If you have a chrome instance already running in debug mode, you can skip the initialization of the Chrome class and just call Chrome.GetTab() directly. For example:

Code: [Select all] [Download] GeSHi © Codebox Plus

#Include Chrome.ahk

TabInst := Chrome.GetTab()
TabInst.Evaluate("alert('hi!');")


What is the replacement for this process with v1.1? GetTab() no longer exists in the class, and PageInst := Chrome.GetPage() returns a "connection with the server could not be established" error.

You're meant to create an instance of the Chrome class before calling GetTab()/GetPage(). If you're already running a copy of chrome with debugging enabled I can see how it could have worked with Chrome.ahk v1.0, but not anymore with v1.1.

If you don't want to launch a new instance of chrome to connect to, you could probably set Chrome.DebugPort := 9222 before calling Chrome.GetPage() and it would behave like you were expecting.




kunkel321 wrote:Thanks for your work on this, GeekDude! It's a bit above my ability to fully understand, but I wonder: Would it be possible to use this for setting the default printer in Chrome? I have a nice AHK script that (at system startup) reads my IP address, then sets my default Windows printer. Chrome doesn't use the Windows default printer though. It merely reverts to whatever printer was last used.

As far as I can tell, there is no way to change the printer settings through this interface.
Shadowpheonix
Posts: 1258
Joined: 16 Apr 2015, 09:41

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

23 Mar 2018, 00:21

GeekDude wrote:
Shadowpheonix wrote:
Joe Glines wrote:
Shadowpheonix wrote:In the "pastebin.ahk" example that GeekDude was nice enough to include in the release package, the Tab.Call("DOM.setAttributeValue", {"nodeId": NameNode.NodeId, "name": "value", "value": "ChromeBot"}) line assigns a value to an input field. How would I go about retrieving the value after it was assigned?

I don't have an example I can post right now but I "solved this" by inserting Java script.
If you happen to come up with an example you can post, it would be most appreciated. :D
Something like MsgBox, % PageInst.Evaluate("document.querySelector('input[name=desc]').value").value should work.


Shadowpheonix wrote:
GeekDude wrote:If you have a chrome instance already running in debug mode, you can skip the initialization of the Chrome class and just call Chrome.GetTab() directly. For example:

Code: [Select all] [Download] GeSHi © Codebox Plus

#Include Chrome.ahk

TabInst := Chrome.GetTab()
TabInst.Evaluate("alert('hi!');")
What is the replacement for this process with v1.1? GetTab() no longer exists in the class, and PageInst := Chrome.GetPage() returns a "connection with the server could not be established" error.
You're meant to create an instance of the Chrome class before calling GetTab()/GetPage(). If you're already running a copy of chrome with debugging enabled I can see how it could have worked with Chrome.ahk v1.0, but not anymore with v1.1.

If you don't want to launch a new instance of chrome to connect to, you could probably set Chrome.DebugPort := 9222 before calling Chrome.GetPage() and it would behave like you were expecting.

Thank you! Both work perfectly, and will help me solve some long standing issues I have had with automating a few web interactions for my work. :superhappy:
User avatar
Joe Glines
Posts: 542
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

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

25 Mar 2018, 07:28

Shadowpheonix wrote:
Joe Glines wrote:However, this still leaves me clueless as to how to read an existing value from a web page in Chrome without having to install Selenium. :lol:


I posted my function for getting text / data from a page with Chrome & AutoHotkey. Check it out and the below video walking through how to use it.
Freire
Posts: 11
Joined: 10 Dec 2017, 11:30

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

28 Mar 2018, 08:10

GeekDude,

Thanks a lot for this tool. This is way better than working with IE or the selenium basic with ahk.

There is a minor bug that I found, when opening multiple url's you have to wait for like 700 ms for the about:blank on page before calling Chrome.GetPage() or it will use the old tab opened to return the object.
Guest

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

04 Apr 2018, 07:01

@joeglines

I haven't studied your Chm_Set() function very closely yet but will you be adding "frame support" to Chm_Set() so you can control forms in say a "content" frame (or frame[2]) document.parentwindow.frames("content").document. :?:
User avatar
Joe Glines
Posts: 542
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

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

04 Apr 2018, 07:23

I wasn't planning on it but that's something that, theoretically, shouldn't be too hard for you to do... I don't anticipate actually using the chrome functionality myself as I do all of my scraping with IE. I'm just excited that the basics are available for Chrome so people can automate logging-in, etc.
User avatar
Lateralus138
Posts: 47
Joined: 30 Aug 2015, 20:52
Facebook: lateralus138
Google: +IanPride
GitHub: Lateralus138
Location: Decatur, IL.
Contact:

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

05 Apr 2018, 15:30

Much appreciated!!! I've been expanding (learning) my AutoHotkey skills more in browser com objects lately and testing and looking through scripts and functions like this help me understand how things work a lot better than plain documentation usually does.

K̴̡̛̻̮̼͕̬̑̋̀̂͆͛̍̑͢ȩ̮̞͍̩̯̋̈͒͌̕ę̶͓̗͖͔̹̪͗̂̈͛̓͘p̠͉̙̟̒̊͌̐͘͘͟͡͞ S̸͖̖̮̞̥͇̖̓̌͛̽̿̓̊̓̾̚͜w͇̮͓̱͇̘̯͆̓͑̋̇̉͜͝i̢͔̝̳̻̱̋̾͐̾͗͊̀̕͜͡͡n̷̡͔̦̤̝̼̩̎͌̈́̀͛̄͆̎͠ǵ̸̘̝̭̦̠̗͖͌͐͑̑̿̅̈͜͜ḯ̡̬̥̙̩̼̪̑͆̿̌́n̛̼͎̲̬͇̲͉̗̞͊̓̃̂̈͝g̸͕̜͖̪͉͔̩̓̃̀̃͌̑̋̕͘.̪̜̜̜̯̂͂̈́͛̆͗̇̍̇.̟͔͍̙̜̫̗̂̿͛͋͋̈́̾̾̿͑.̡̣̟̝̭͉̦̪́̓̀͛̑̓̐̈͘͘

KusochekDobra
Posts: 32
Joined: 25 Apr 2016, 18:00

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

11 Apr 2018, 18:13

It's a good job!!! Great thanks!!!

But how can i add event listener in JavaScript-code, which launches the functions in AHK-code?
Next simple example shows the coordinates of a click in a fixed div-block, if click on the button "Click inform" when the page loads:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


How to call a AHK-function that shows this coordinates of a click in "MsgBox"?

Update:
If I understand correctly, the ActiveX control in the object of the class acts as a server for the browser. Then how do I send a message from the browser page?
Then it could be processed in "ws.onmessage". Right?
KusochekDobra
Posts: 32
Joined: 25 Apr 2016, 18:00

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

18 Apr 2018, 17:26

The only trouble-free solution that we managed to find is to activate the console domain:
PageInst.Call("Console.enable")
And catch incoming messages "Console.messageAdded"
I had to change the "Chrome" class a little, adding the sixth parameter to the call constructor, here and there to transfer it further for the nested class "Page", and rename it to "PageBase" and extend him, redefining the constructor and the method "Event":

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Thus, if set the page "Click informer", as well press the button "Get event data" then when the clicks will be called function MSGFunc(), getting the same text as div-tag.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


May I ask you to add this opportunity to the future editions of your work? Having a this opportunity would be very convenient.

And sorry for my English. =/
GeekDude
Posts: 827
Joined: 02 Oct 2013, 22:13

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

21 Apr 2018, 22:38

KusochekDobra wrote:The only trouble-free solution that we managed to find is to activate the console domain:
PageInst.Call("Console.enable")
And catch incoming messages "Console.messageAdded"
I had to change the "Chrome" class a little, adding the sixth parameter to the call constructor, here and there to transfer it further for the nested class "Page", and rename it to "PageBase" and extend him, redefining the constructor and the method "Event"

...

May I ask you to add this opportunity to the future editions of your work? Having a this opportunity would be very convenient.

And sorry for my English. =/


This exactly the kind of thing I had in mind, but had not gotten around to implementing it. It's definitely on the to-do list to get this into the library, though rather than passing it into the constructor I might have it as a class property you would set (ChromeInst.OnConsole := Func("MyHandler") for example). I'll have to take a long look into it to make sure it's as future-proof and extensible as it needs to be. Who knows, I might incorporate your ideas directly :)

Your work is very good, and so is your English. Thank you for sharing with us!




And now for some hype

Image
KusochekDobra
Posts: 32
Joined: 25 Apr 2016, 18:00

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

22 Apr 2018, 04:53

Oh, it's so great!!!

In a large family AHK it is customary to be generous to knowledge. It's nice to be a part of this!
Look forward to continuing! :thumbup:
yawikflame
Posts: 20
Joined: 02 Jan 2017, 06:19

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

24 Apr 2018, 15:09

Awesome stuff GeekDude!
small question,
is there a way to retrieve the source code of the current working tab?
thank you so much for this great code!!
cheers ;)
yawik
User avatar
Xtra
Posts: 1142
Joined: 02 Oct 2015, 12:15

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

24 Apr 2018, 17:21

yawikflame wrote:is there a way to retrieve the source code of the current working tab?

PageInst.Evaluate("document.getElementsByTagName('html')[0].textContent;").Value
KusochekDobra
Posts: 32
Joined: 25 Apr 2016, 18:00

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

24 Apr 2018, 19:25

If under the control of the script will be several pages and all of them will be send a messages, then we need implement the concept of the event stack for make a manage all of them without transmittance. For example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


While MsgBox is showed, we can more press Ctrl+LButton and all other coordinates of Click, will be showed later.
I suggest adding event processing by default in Class Page and add external simple class EventStacker, who will do this:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


This will be convenient for the developer in that it will not interfere in the code of Class Chrome. For change main behavior, it will only be necessary to make changes to his method PopEvent(), and the Class EventStacker itself can be easily put in the body of the main script, for easy editing. Then, you just need to make a couple of simple changes, first, in the constructor Class Page, for example:

Code: [Select all] [Download] GeSHi © Codebox Plus

this.e := New EventStacker()

And the second, in the condition else if (EventName == "Message") of the method Event(), of the same class. For example:

Code: [Select all] [Download] GeSHi © Codebox Plus

else if (EventName == "Message")
{
data := Chrome.Jxon_Load(Event.data)
if (data.method == "Console.messageAdded" && (msg := data.params.message).level == "info")
this.e.InsertEvent(msg.text)
if this.responses.HasKey(data.ID)
this.responses[data.ID] := data
}

Thus, by default, if the console has been enabled, all events from the browser console at the level "info", will be added at event stack.

It would be great to see a something similar. :)

Edit:
More precisely not a stack, but a queue. :)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 10 guests