[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
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

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

25 Jan 2018, 17:04

- Is there a way to get the urls for all open tabs directly? Instead of Ctrl+Tab and using Acc/Ctrl+C.
- Many thanks for this GeekDude.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
geek
Posts: 1051
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

25 Jan 2018, 23:14

DanielToward13 wrote:Thanks GeekDude. That’s great!
:)
DanielToward13 wrote:is there a way to get the scroll bar position in order to know where in the page the current view is?
You could run some javascript on the page that gets the scroll position and use the returned value. For example:

Code: Select all

Loop
{
    Position := Tab.Evaluate("window.pageYOffset / (document.body.clientHeight - window.innerHeight)").value
    ToolTip, %Position%
    Sleep, 500
}

jeeswg wrote:- Is there a way to get the urls for all open tabs directly? Instead of Ctrl+Tab and using Acc/Ctrl+C.
Chrome.GetTabs() returns an array of all loaded tabs/pages/documents (including also extension configuration pages).

Code: Select all

for Index, Tab in Chrome.GetTabs()
    MsgBox, % "type: " Tab.type "`nurl: " Tab.url
jeeswg wrote:- Many thanks for this GeekDude.
:)
ibbs

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

02 Mar 2018, 23:31

Good stuff, was wondering if anyone has played around with any chromecast automation yet? Haven't had much luck here...
User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

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

03 Mar 2018, 05:03

First fix was easy. Simply add https://github.com/G33kDude/WebSocket.ahk , https://github.com/cocobelgica/AutoHotkey-JSON to the already existing (empty) Folders.

But then i got this error mesage by starting example https://github.com/G33kDude/Chrome.ahk/ ... jectJS.ahk
This error happens inside Jxon_Dump(obj, indent:="", lvl:=1){ ... https://github.com/cocobelgica/AutoHotk ... n.ahk#L174

Code: Select all

------- AHKVersion 1.1.28.00 --------------------
InjectJS.ahk > Jxon.ahk (174) : ==> Call to nonexistent function.
Specifically: Jxon_Dump(k) : q . k . q ) .  ( indent ? ": " : ":" )
>Exit code: 2    Time: 0.5602
i dont know why this recursive call not works. hmm
geek
Posts: 1051
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

05 Mar 2018, 13:30

SL5 wrote:First fix was easy. Simply add https://github.com/G33kDude/WebSocket.ahk , https://github.com/cocobelgica/AutoHotkey-JSON to the already existing (empty) Folders.

But then i got this error mesage by starting example https://github.com/G33kDude/Chrome.ahk/ ... jectJS.ahk
This error happens inside Jxon_Dump(obj, indent:="", lvl:=1){ ... https://github.com/cocobelgica/AutoHotk ... n.ahk#L174

Code: Select all

------- AHKVersion 1.1.28.00 --------------------
InjectJS.ahk > Jxon.ahk (174) : ==> Call to nonexistent function.
Specifically: Jxon_Dump(k) : q . k . q ) .  ( indent ? ": " : ":" )
>Exit code: 2    Time: 0.5602
i dont know why this recursive call not works. hmm
Chrome.ahk depends on this specific version https://github.com/G33kDude/AutoHotkey- ... 38238f81e1 of AutoHotkey-JSON that has been forked and modified from Cocobelgica's original to allow for Boolean JSON values and to allow it to exist alongside other versions of AutoHotkey-JSON. You can find that version by clicking through the repository on the GitHub webpage, or by going to the releases tab and downloading the release which includes all of the required dependencies.
GeekDude wrote: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
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

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

08 Mar 2018, 19:41

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?

Looking over the protocol documentation, there does not appear to be a DOM.getAttributeValue or anything similar. The following snippet is the best I could come up with, but all I get is a single message box with "attributes" = ""...

Code: Select all

NameNode := Tab.Call("DOM.querySelector", {"nodeId": RootNode.nodeId, "selector": "input[name=name]"})
Tab.Call("DOM.setAttributeValue", {"nodeId": NameNode.NodeId, "name": "value", "value": "ChromeBot"})

TheNode := Tab.Call("DOM.getAttributes", {"nodeId": NameNode.NodeId})
For k, v in TheNode
	MsgBox "%k%" = "%v%"
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

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

08 Mar 2018, 20:16

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.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
printer

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

11 Mar 2018, 13:43

I want to do Page.printToPDF with a few parameters on a local .html file. Can Chrome.ahk help me do that without rendering the .html in the Chrome window? Any code examples for that would be very appreciated. I want to do the conversion with as little delay as possible.

There is also the simpler and headless command line method

Code: Select all

chrome --headless --disable-gpu --print-to-pdf="C:/test/1.pdf" "C:\test\1.html"
But apparently the printToPDF parameters aren't and won't be implemented for the command line command.
Guest

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

13 Mar 2018, 12:24

Any particular reason to use Chrome to print pdfs? There are headless solutions which easy scripting options:

* http://phantomjs.org/ (google phantomjs print pdf for example scripts, you don't need to install it, just unzip the download)
* https://github.com/wkhtmltopdf/wkhtmltopdf/
User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

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

13 Mar 2018, 15:12

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.
ste(phen|ve) kunkel
printer

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

14 Mar 2018, 05:51

Guest wrote:Any particular reason to use Chrome to print pdfs? There are headless solutions which easy scripting options:

* http://phantomjs.org/ (google phantomjs print pdf for example scripts, you don't need to install it, just unzip the download)
* https://github.com/wkhtmltopdf/wkhtmltopdf/
No super strong reason really.
But whtmltopdf hasn't been updated since 2016 and the Phantomjs main dev have called it quits and says Headless Chrome is the future.

So I figure I might as well try to learn to work with Headless Chrome. Chrome.ahk looks like a convenient way to start for me, compared to going down the Node and Puppeteer path.
User avatar
ramonstart
Posts: 16
Joined: 26 Apr 2016, 09:00

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

14 Mar 2018, 09:05

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/livro ... 20Kiyosaki

Would you help me?
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

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

16 Mar 2018, 18:23

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
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

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

16 Mar 2018, 18:34

Shadowpheonix wrote:If you happen to come up with an example you can post, it would be most appreciated. :D
I'll try and work on it this weekend. I was in the middle of converting all of my examples to functions (so they'd be easy to adapt) when I got busy...
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

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

17 Mar 2018, 14:21

Okay, I created a function which should help some people (noobs). It leverages GeekDude's Chrome class.

You can get the function here

And this is a video walking through how to use it. It isn't great, but will help some people get their feet wet. :D
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
fenchai
Posts: 290
Joined: 28 Mar 2016, 07:57

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

18 Mar 2018, 22:07

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:
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

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

19 Mar 2018, 12:37

Joe Glines wrote:Okay, I created a function which should help some people (noobs). It leverages GeekDude's Chrome class.

You can get the function here

And this is a video walking through how to use it. It isn't great, but will help some people get their feet wet. :D
This function definitely has a lower learning curve to it than Tab.Call() and Tab.Evaluate() for writing values. Thank you for posting it. :bravo:

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:
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

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

19 Mar 2018, 12:42

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.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 80 guests