ChromeInst.GetPage seems to be failing (Chrome.ahk)

Get help with using AutoHotkey and its commands and hotkeys
SupposedToBeWorking
Posts: 34
Joined: 08 Mar 2017, 16:15

ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 11:09

Hello! I am trying to use g33kDude's Chrome.ahk library and I can't seem to instantize the page. Here is my code:

Code: Select all

#Persistent
#SingleInstance, force
#NoEnv
SetBatchLines, -1
SetTitleMatchMode, 2
SetWorkingDir, %A_ScriptDir%
#Include Chrome.ahk

SetTimer, createChromeObj, 1000
SetTimer, waitForClose, off
Return

;-----------CREATE CHROME OBJECT--------------------
createChromeObj:
SetTimer, createChromeObj, off
FileCreateDir, ChromeProfile
ChromeInst := new Chrome("ChromeProfile","https://www.google.com/"," --remote-debugging-port=9222 --disable-web-security")

h_WinTitle:="Google - Google Chrome" 
WinWait, % h_WinTitle
WinGet, h_WinID, ID, % h_WinTitle
h_WinID := "ahk_id " . h_WinID
SetTimer, waitForClose, 3000

while !(PageInst := ChromeInst.GetPage())
{
	tooltip, could not retrieve page!
	Sleep 1000
}
tooltip, connected to page!
PageInst.WaitForLoad()

;----------EXECUTE JAVASCRIPT--------
PageInst.Evaluate("alert('Hello World!');")
Return

waitForClose:
If !WinExist(h_WinID){
	ExitApp
}
Return
The page is never instantized and so the tooltip is always " could not retrieve page!" and the JavaScript never executes. Any help is appreciated!
teadrinker
Posts: 2183
Joined: 29 Mar 2015, 09:41
Contact:

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 11:37

Hi

Did you check if ChromeInst is an object?
gregster
Posts: 5837
Joined: 30 Sep 2013, 06:48

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 11:45

Seems to work here, although I don't see the point of the timers.

Also, Chrome responds to the use of the command line flag "--disable-web-security" with the message that it is not supported here and that this could negatively affect browser stability and security. But the javascript and the tooltip work anyway.

But a Chrome instance with a google.com page is opening for you?

What about opening the Chrome browser first with a blank page, then getting this blank page and navigating it to google.com ?
(this is my preferred way of doing it; you can still create more tabs later)
SupposedToBeWorking
Posts: 34
Joined: 08 Mar 2017, 16:15

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 13:41

teadrinker wrote:
31 May 2019, 11:37
Did you check if ChromeInst is an object?
Hey, I added this in and ran it again.

Code: Select all

if !(ChromeInst){
	msgbox, chrome is not an object
}
I did not get an error, so this does not appear to be the issue. It is most def PageInst := ChromeInst.GetPage() but I'm not sure why it's failing.
teadrinker
Posts: 2183
Joined: 29 Mar 2015, 09:41
Contact:

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 13:59

For me your code works. Try what gregster suggested.
SupposedToBeWorking
Posts: 34
Joined: 08 Mar 2017, 16:15

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 14:02

gregster wrote:
31 May 2019, 11:45
Seems to work here..
That is the part that worries me haha! I am not sure what's up with my setup that is preventing it from working.
gregster wrote:
31 May 2019, 11:45
...although I don't see the point of the timers
I'm just using them as an easy way to exit the script when I close chrome.
gregster wrote:
31 May 2019, 11:45
...Also, Chrome responds to the use of the command line flag "--disable-web-security" with the message that it is not supported here and that this could negatively affect browser stability and security. But the javascript and the tooltip work anyway.
I added that flag because I'll be injecting some JavaScript later.
gregster wrote:
31 May 2019, 11:45
...But a Chrome instance with a google.com page is opening for you?
Yes indeed. The google opens but it is not instantized.
gregster wrote:
31 May 2019, 11:45
What about opening the Chrome browser first with a blank page, then getting this blank page and navigating it to google.com ?
(this is my preferred way of doing it; you can still create more tabs later)
Here is my updated coded adding in that suggestion:

Code: Select all

#Persistent
#SingleInstance, force
#NoEnv
SetBatchLines, -1
SetTitleMatchMode, 2
SetWorkingDir, %A_ScriptDir%
#Include Chrome.ahk

;-----------CREATE CHROME OBJECT--------------------
createChromeObj:
SetTimer, createChromeObj, off
FileCreateDir, ChromeProfile
ChromeInst := new Chrome("ChromeProfile",," --remote-debugging-port=9222 --disable-web-security")

if !(ChromeInst){
	msgbox, chrome is not an object
}

while !(PageInst := ChromeInst.GetPage())
{
	tooltip, could not retrieve page!
	Sleep 1000
}
tooltip, connected to page!
PageInst.WaitForLoad()

;----------EXECUTE JAVASCRIPT--------
PageInst.Evaluate("alert('Hello World!');")
Return
This still does not work for me. Any other suggestions? I really appreciate the help!
SupposedToBeWorking
Posts: 34
Joined: 08 Mar 2017, 16:15

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 14:06

teadrinker wrote:
31 May 2019, 13:59
For me your code works. Try what gregster suggested.
Thanks for testing it for me! Since I have the must up-to-date AHK and Chrome.ahk... I think it is safe to say something is going on with Chrome? Have you enabled debug mode in Chrome? If so, did you do it by going to:
Chrome > Three Dot Menu > More Tools > Extensions > Developer mode toggle

I appreciate the help!
gregster
Posts: 5837
Joined: 30 Sep 2013, 06:48

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 14:20

Chrome.ahk starts your Chrome browser in debugging mode (but I think it can also connect to an already existing debugging instance of Chrome, although I have never tried it). Do you run other (non-debug) instances of Chrome with the same profile at the same time? Then, try to close them before or create a new profile for your Chrome debugging session...

Apart from that, I have no current idea.

Like I said, the command line parameter you are using is not supported (at least not for a debugging sesssion) - so it should have no (productive) effect. I have used javascript several times before in Chrome.ahk and I never needed a special command line parameter, since Chrome was already in debugging mode.
teadrinker
Posts: 2183
Joined: 29 Mar 2015, 09:41
Contact:

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 14:24

SupposedToBeWorking wrote: Have you enabled debug mode in Chrome? If so, did you do it by going to:
Chrome > Three Dot Menu > More Tools > Extensions > Developer mode toggle
No, in my Chrome the developer mode is disabled.
Stavencross
Posts: 82
Joined: 24 May 2016, 16:42

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 14:24

SupposedToBeWorking wrote:
31 May 2019, 11:09
Hello! I am trying to use g33kDude's Chrome.ahk library and I can't seem to instantize the page. Here is my code:

Code: Select all

#Persistent
#SingleInstance, force
#NoEnv
SetBatchLines, -1
SetTitleMatchMode, 2
SetWorkingDir, %A_ScriptDir%
#Include Chrome.ahk

SetTimer, createChromeObj, 1000
SetTimer, waitForClose, off
Return

;-----------CREATE CHROME OBJECT--------------------
createChromeObj:
SetTimer, createChromeObj, off
FileCreateDir, ChromeProfile
ChromeInst := new Chrome("ChromeProfile","https://www.google.com/"," --remote-debugging-port=9222 --disable-web-security")

h_WinTitle:="Google - Google Chrome" 
WinWait, % h_WinTitle
WinGet, h_WinID, ID, % h_WinTitle
h_WinID := "ahk_id " . h_WinID
SetTimer, waitForClose, 3000

while !(PageInst := ChromeInst.GetPage())
{
	tooltip, could not retrieve page!
	Sleep 1000
}
tooltip, connected to page!
PageInst.WaitForLoad()

;----------EXECUTE JAVASCRIPT--------
PageInst.Evaluate("alert('Hello World!');")
Return

waitForClose:
If !WinExist(h_WinID){
	ExitApp
}
Return
The page is never instantized and so the tooltip is always " could not retrieve page!" and the JavaScript never executes. Any help is appreciated!

I had this exact same problem, it started a bit ago (while I wasn't working on my chrome.ahk script). It worked when chrome.ahk first came out and then it just started to bust. I tore my hair out and then I realized I was essentially trying to connect to the page before the GUI had time to finish drawing (They've slowed chrome down a lot lately with all the security changes). Adding a sleep fixed it for me

Code: Select all

FileCreateDir, ChromeProfile ;create a debugging profile of chrome
ChromeInst := new Chrome("ChromeProfile", "--app=http://localhost/index.php") ;We'll run chrome in "app" mode, which strips out all the UI buttons. We can run a reg instance by removing --app=
sleep, 3000
PageInst := ChromeInst.GetPageByURL("http://localhost/index.php",,, "event_Callback") ;connect chrome.ahk to the google chrome tab
PageInst.Call("Console.enable") ;watch the console for messages served via javascript, this is how our server will communicate.
*Edited to fix the sleep amount that I fat fingered
gregster
Posts: 5837
Joined: 30 Sep 2013, 06:48

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 14:29

You wait 30 seconds? Wow, on my rather old computer, it is usually sufficient to use WinWait, Google Chrome, but I always open it with a blank page, before I start navigating.

Edit: oh, I see you changed it now to 3 - that's more like it ;)
SupposedToBeWorking
Posts: 34
Joined: 08 Mar 2017, 16:15

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

31 May 2019, 14:43

gregster wrote:
31 May 2019, 14:20
Chrome.ahk starts your Chrome browser in debugging mode (but I think it can also connect to an already existing debugging instance of Chrome, although I have never tried it). Do you run other (non-debug) instances of Chrome with the same profile at the same time? Then, try to close them before or create a new profile for your Chrome debugging session...
Ah ok, I did not know Chrome.ahk did that for me. When I remove the " --remote-debugging-port=9222 --disable-web-security" I have the same issue. I have deleted and recreated the ChromeProfile and still no dice, so I know that's not it.
teadrinker wrote:
31 May 2019, 14:24
No, in my Chrome the developer mode is disabled.
I disabled Developer Mode in mine too, tried again and still will not work. Based on the comment from gregster Chrome.ahk enabled debug already anyway.

Stavencross wrote:
31 May 2019, 14:24
I had this exact same problem, it started a bit ago (while I wasn't working on my chrome.ahk script). It worked when chrome.ahk first came out and then it just started to bust. I tore my hair out and then I realized I was essentially trying to connect to the page before the GUI had time to finish drawing (They've slowed chrome down a lot lately with all the security changes). Adding a sleep fixed it for me

Code: Select all

FileCreateDir, ChromeProfile ;create a debugging profile of chrome
ChromeInst := new Chrome("ChromeProfile", "--app=http://localhost/index.php") ;We'll run chrome in "app" mode, which strips out all the UI buttons. We can run a reg instance by removing --app=
sleep, 3000
PageInst := ChromeInst.GetPageByURL("http://localhost/index.php",,, "event_Callback") ;connect chrome.ahk to the google chrome tab
PageInst.Call("Console.enable") ;watch the console for messages served via javascript, this is how our server will communicate.
I completely understand this logic, and had the same issue myself before and this solution solved it. However, I gave this a shot and I was still unable to get PageInst... so there must be something else. I may try testing on a different computer.
SupposedToBeWorking
Posts: 34
Joined: 08 Mar 2017, 16:15

Re: ChromeInst.GetPage seems to be failing (Chrome.ahk)

21 Jun 2019, 13:27

UPDATE:

This works after reboot! Why? No idea!

Return to “Ask For Help”

Who is online

Users browsing this forum: Arsonistic, Bing [Bot], mikeyww, nacken012, newbieforever, paul1965 and 49 guests