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 {}
}