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

Get help with using AutoHotkey (v1.1 and older) 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: 4412
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: 9113
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: 4412
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: 9113
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: 4412
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: 90
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: 9113
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!
tester
Posts: 84
Joined: 10 Jun 2021, 23:03

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

15 Sep 2022, 00:36

I've run into a similar problem which the ChromeInst.GetPage() method starts not returning anything. Then I did some try and errors.

In my case, when the --headless option is enabled and the page is not closed properly, it seems to start occurring, but not always. But at least I seem to have figured how to connect to an orphaned debug instance and close it so that ChromeInst.GetPage() starts returning a page object.

This creates an orphaned browser instance.

Code: Select all

#Include <Chrome>
iPort     := 9229
sHeadless := " --headless"
sURL      := "https://google.com"

FileCreateDir, %A_ScriptDir%\ChromeProfile%iPort% 
ChromeInst := new Chrome(A_ScriptDir "\ChromeProfile" iPort,, "--no-first-run" sHeadless,, iPort)

PageInstance := ChromeInst.GetPage()
PageInstance.Call("Page.navigate", {"url": sURL})
PageInstance.WaitForLoad()
sURLNavigated := PageInstance.Evaluate("(() => { return window.location.href.toString(); })();").value
PageInstance.Call("Page.close") ; to close the connection properly in the headless mode, use PageInstance.Call("Browser.close") but it closes all other tabs
PageInstance.Disconnect()

msgbox % "Request: " sURL "`n"
    . "Navigated: " sURLNavigated "`n"
ExitApp
From a different script, run this and press F2 to check if the debugger is running. Then F3 to close the connection and browser processes associated with the port.

Code: Select all

#Include <Chrome>

iPort := 9229
FileCreateDir, % A_ScriptDir "\ChromeProfile" iPort
Return

~Esc::ExitApp

; Detect orphaned instances
F2::ShowDebuggerInfo(iPort)

; Clean up
F3::     
    CloseDebuggerProcesses(iPort)
    ShowDebuggerInfo(iPort)
Return

ShowDebuggerInfo(iPort){
    oDebugBrowserInfo  := GetDebugBrowserInfo(iPort)
    oDebuggerProcesses := GetDebugBrowserProcessesByPort(iPort)
    for iPID in oDebuggerProcesses
        sPIDs .= iPID ","
    msgbox % "Debugger is Running: " (oDebugBrowserInfo.Count() ? "Yes" : "No") "`n"
        . "WebSocket Debugger URL: " oDebugBrowserInfo.webSocketDebuggerUrl "`n"
        . "Debugger Processes: total " oDebuggerProcesses.Count() ", " RTrim( sPIDs, "," ) "`n"        
}

CloseDebuggerProcesses(iPort:=9222,sPathExe:="chrome.exe") {
    sWebSocketDebuggerURL := GetDebugBrowserInfo(iPort).webSocketDebuggerUrl
    if (sWebSocketDebuggerURL) {
        ; Connect to the page
        oPage := new Chrome.Page(sWebSocketDebuggerURL)
        if oPage.Connected {
            oPage.Call("Browser.close")
            oPage.Disconnect()        
        }
    }    
    ; Find orphaned browser processes and kill them
    for iPID in GetDebugBrowserProcessesByPort(iPort)
        Process, Close, % iPID
}

GetDebugBrowserProcessesByPort(iPort:=9222, sPathExe:="chrome.exe") {
    _oOut := {}
    For _oItem in ComObjGet("winmgmts:").ExecQuery("SELECT CommandLine,ProcessId FROM Win32_Process WHERE Name = '" sPathExe "'")
        If RegExMatch(_oItem.CommandLine, "--remote-debugging-port=(" iPort ")") && ! InStr( _oItem.CommandLine, "--type=" )    ; ignore child processes by checking `--type=`
            _oOut[_oItem.ProcessId] := _oItem.CommandLine
    return _oOut
}	
GetDebugBrowserInfo(iPortDebug:=9222) {
    return GetJSONHTTPResponse("http://127.0.0.1:" iPortDebug "/json/version")
}
; Not used here
; GetPageList(iPortDebug:=9222) {
;     return GetJSONHTTPResponse("http://127.0.0.1:" iPortDebug "/json")
; }	 
GetJSONHTTPResponse(sURL, aRequestTimeout:="", iResponseTimeout:=10) {
    aRequestTimeout := IsObject( aRequestTimeout ) ? aRequestTimeout : [ 10000, 10000, 10000, 30000 ]
    _oHttp          := ComObjCreate("WinHttp.WinHttpRequest.5.1")
    _oHttp.SetTimeouts(aRequestTimeout*)
    Try {
        _oHttp.Open("GET", sURL, true)
        _oHttp.Send()
        _oHttp.WaitForResponse(iResponseTimeout)
        if ( 200 != _oHttp.Status ) {
            throw Exception(_oHttp.Status)
        }
        return Chrome.Jxon_Load(_oHttp.responseText)
    } 
    return {}
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 365 guests