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

Post your working scripts, libraries and tools
bmilcs
Posts: 5
Joined: 27 Feb 2017, 13:54

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

25 Jun 2018, 10:47

I have a few questions for you regarding this script:

#1) PageInst.Call("Page.navigate", {"url": address})

I am trying to pass text that isn't a specific URL, but text that I would normally type to search google for. However, I'm receiving errors from Chrome.AHK. Is there a section of the Chrome.ahk script I can edit to allow for non-url text to be passed as valid?

#2) How do I retrieve the current URL of the active PageInst?

#3) I am a dummy, but are there commands that work with Chrome.ahk that aren't mentioned as examples?
I have zero experience with this, and am wondering if there is another resource detailing the COMS available to us via Chrome.ahk?
In other words, does this just unlock the ability to use Chrome and not give you a full scope of possibilities?

I'm dying to learn more but feel like I'm missing something. Thanks!

Edit: Apologies if I misused vocab or sound like a moron. I've been able to successfully scrape metadata off of websites which is quite exciting! Thank you again for posting such a tremendously useful script!
GeekDude
Posts: 827
Joined: 02 Oct 2013, 22:13

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

25 Jun 2018, 11:06

Manuf

Manuf wrote:Hi sirs. Can you help me with this simple task

i want to write a script to detect
- can check if webpage full loaded
- can check if webpage loaded error
- can check if an element display after an action. Ie: after upload a file, a button will turn from gray to yellow. i want to check that state of the button.

Thanks you

I'm sorry but I can't help with this without a much more thorough understanding of your situation and your goals. I don't have the time to dive that deeply into it right now.

You should be able to check the page readiness state by running PageInst.Evaluate("document.readyState"), which should return how loaded the page is. For the rest of your post I'm not sure.




bmilcs

bmilcs wrote:I have a few questions for you regarding this script:

#1) PageInst.Call("Page.navigate", {"url": address})

I am trying to pass text that isn't a specific URL, but text that I would normally type to search google for. However, I'm receiving errors from Chrome.AHK. Is there a section of the Chrome.ahk script I can edit to allow for non-url text to be passed as valid?

I don't think you can do that directly, but a viable alternative may be to generate a google search url and navigate to that. For example,

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

PageInst.Call("Page.navigate", {"url": "https://google.com/search?q=" UriEncode("Search Terms")})

; Modified by GeekDude from http://goo.gl/0a0iJq
UriEncode(Uri, RE="[0-9A-Za-z]") {
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0), StrPut(Uri, &Var, "UTF-8")
While Code := NumGet(Var, A_Index - 1, "UChar")
Res .= (Chr:=Chr(Code)) ~= RE ? Chr : Format("%{:02X}", Code)
Return, Res
}




bmilcs wrote:#2) How do I retrieve the current URL of the active PageInst?

Try PageInst.Evaluate("window.location.href")



bmilcs wrote:#3) I am a dummy, but are there commands that work with Chrome.ahk that aren't mentioned as examples?
I have zero experience with this, and am wondering if there is another resource detailing the COMS available to us via Chrome.ahk?
In other words, does this just unlock the ability to use Chrome and not give you a full scope of possibilities?

As mentioned at the top of the original post, the Chrome DevTools Protocol's documentation lists a lot of endpoints that you can use from PageInst.Call: https://chromedevtools.github.io/devtools-protocol/



bmilcs wrote:I'm dying to learn more but feel like I'm missing something. Thanks!

Edit: Apologies if I misused vocab or sound like a moron. I've been able to successfully scrape metadata off of websites which is quite exciting! Thank you again for posting such a tremendously useful script!

No need to apologize. I love when people are eager to learn :) . Good luck with your code!
bmilcs
Posts: 5
Joined: 27 Feb 2017, 13:54

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

01 Jul 2018, 17:58

Here's a fun question for you:

Image

Image

Now, I've figured out that...

PageInst.Evaluate("document.getElementsByTagName('INPUT')[2].value = ' HOUR HERE '")
PageInst.Evaluate("document.getElementsByTagName('INPUT')[4].value = ' MIN HERE '")

So, I am able to populate the hour and minutes field. However, I am baffled as to how to alter the Listbox's value, and set AM or PM.

#1: How do I set the AM PM value in the above Listbox?

;-----------------------------------------------------------------------------------------------------------------------------

ANDDD #2:

Image

Image

#2 How do I apply a check in a checkbox?

#3 Lastly, how can I identify an Element by an aria-label, or other non-standard classification? IE: Not TagName, ID, etc.

IE: Set the value of a field, finding it via "aria-label"

BIG THANKS TO G33KDUDEBROMAN. This sh** is BIG TIME.
bmilcs
Posts: 5
Joined: 27 Feb 2017, 13:54

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

02 Jul 2018, 14:15

Sorry if my questions are simplistic once again. I am brand new to DOM and slowly grasping the core concepts.


If CurUrl not contains http://www.amazon.com
ChromeGoUrl("https://www.amazon.com/Punch-Out-Nintendo-Selects-Wii/dp/B005EIIBKY/ref=sr_1_1?s=hi&ie=UTF8&qid=1530544404&sr=8-1&keywords=punchout+nintendo")
PageInst.WaitForLoad()
xx := PageInst.Evaluate("document.getElementById('priceblock_ourprice')[0].innerText")

Image

I cannot figure out why this is giving me an error. Within priceblock-ourprice, there are childnodes which I tried grabbing individually using:

document.getElementById('priceblock_ourprice).childNodes[0].innerText
document.getElementById('priceblock_ourprice).childNode[0].innerText

No luck.

Web site source: https://www.amazon.com/Punch-Out-Ninten ... t+nintendo

Thank you all again! You've helped me tremendously!
bmilcs
Posts: 5
Joined: 27 Feb 2017, 13:54

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

02 Jul 2018, 18:14

And as I wear out my welcome here, I think I've found a bug of some type. After I make a dozen or so DOM JS calls, AHK locks up and this is what I'm seeing for output:

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



Those are the last lines every single time it locks up. I've tried running the same getelementby syntax, which I know works for a fact, several times and it locks up at the same point every time.

;-----------------------------------------------------------------------------------------------------------------------------

EDIT: SOLVED.

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

PageInst.Disconnect()


After hours of tinkering with the code and browsing the Chrome DevTools page, I stumbled upon Page.Disconnect(). Apparently, the connection with Chrome was hitting the max allotment of time or only allows for X amount of actions per connection.

I added this to the end of the cycle of actions that I perform. This has completely fixed all issues I was having in regards to AHK freezing up after X amount of lines or time passed.

Hopefully this helps someone <3

The syntax
Manuf

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

09 Jul 2018, 04:23

To @Geekdude

Thanks for your answer. May you help me with this code?

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



I want to get the x coordinate value of a button but after i run the javascript function ( it works ), i cant assign it to a variable. How can i get the "x = react.left" value in autohotkey ?

Thanks you so much

p.s: Sorry if i cant say it clearly. If anyone know the answer, pls tell me. Thanks

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 17 guests