[Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!
-
- Posts: 139
- Joined: 26 Jan 2016, 16:05
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
Hey @malcev -
I noticed that as well - and did some testing with it, appears to work fine. I have alot of other scripts still using console. Do you think there are any performance/reliability issues with leaving those programs as is?
I noticed that as well - and did some testing with it, appears to work fine. I have alot of other scripts still using console. Do you think there are any performance/reliability issues with leaving those programs as is?
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
I dont know, but if it is deprecated then it is possible that it will be removed from feature chrome releases.
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
@Tre4shunter
Yeah I had that earlier in my code, and it would always throw an error:
So I replaced it and since I got no error, I just left it such.
@malcev Yes! I saw the deprecation message on that page too. But because I'm using an older portable version of Chrome, I figured maybe it's still there on mine - though I didn't dig deeper to see in which version it was dropped, if at all.
Yeah I had that earlier in my code, and it would always throw an error:
Code: Select all
Chrome indicated error in response
Specifically: {"code":-32601,"message":"'Console.Enable' wasn't found"}
Line#
481: response := this.responses.Delete(ID)
482: if (response.error)
---> 483: Throw,Exception("Chrome indicated error in response",, Chrome.Jxon_Dump(response.error))
485: Return,response.result
@malcev Yes! I saw the deprecation message on that page too. But because I'm using an older portable version of Chrome, I figured maybe it's still there on mine - though I didn't dig deeper to see in which version it was dropped, if at all.
-
- Posts: 139
- Joined: 26 Jan 2016, 16:05
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
Not sure why you're getting that error. Im using Runtime now, and here is a snipped of my larger program with listenting to the messages - works excellent.
EDIT**** Just realized your error is because its case sensitive. ***********
PageInst.Call("Console.enable") will work, while PageInst.call("Console.Enable") WILL NOT.
Thanks
Code: Select all
Profile := A_Temp "\ChromeProfile"
if !FileExist(Profile)
FileCreateDir, % Profile
ChromeInst := new Chrome(profile,"--app=" Path, "--window-size=1010," Height " --disable-web-security --test-type --bwsi --disable-component-extensions-with-background-pages","",port)
WinActivate, QuoteFormOriginal
While !WinExist("QuoteFormOriginal")
sleep, 100
BoundCallback := Func("Callback").Bind()
PageInstCreate := ChromeInst.GetPageByURL("file:///" StrReplace(Path,"\","/"), "exact",,BoundCallBack)
if !PageInstCreate
{
ChromeInst.Kill()
MsgBox, Could not Connect to Quote Page!
ExitFunc()
}else{
PageInstCreate.Call("Runtime.enable")
if(XL.activesheet.name = "quote ")
{
WinShow, % DocTitle
WinActivate, % DocTitle
}
}
return
ESC:
ExitFunc()
return
Callback(event){
GLOBAL
ahkpath := "F:\userfolders\md\autohotkey\autohotkey.exe"
if(Event.Method = "Runtime.consoleAPICalled"){
Arg := Event.params.args[1].value
InStr(Arg,"AHK:Print") ? Print(Arg)
: InStr(Arg,"AHK:OpenSaved") ? SearchQuotes(Arg)
: InStr(Arg,"AHK:PageReady") ? pageReady()
: InStr(Arg,"AHK:openconfigselect") ? pageReady(Arg)
: InStr(Arg,"AHK:OpenQuote") ? OpenQuote(Arg)
: InStr(Arg,"AHK:PasteTM") ? PasteTMData()
: InStr(Arg,"AHK:CopyLines") ? (clipboard := StrSplit(Arg,"~:").2)
: InStr(Arg,"AHK:PasteLines") ? PasteDirectLineItems()
: InStr(Arg,"AHK:email") ? sendEmail(Arg)
: InStr(Arg,"AHK:runsiemensservice") ? RunPath(ahkpath " F:\notebook\prices\Configurator_Scripts\QuoteForm\Service\ServiceV2.ahk """ Doctitle """ " ScriptHwnd """", "F:\notebook\prices\Configurator_Scripts\QuoteForm\Service\")
: InStr(Arg,"AHK:ResetQuote") ? ResetQuote()
: InStr(Arg,"AHK:Import") ? LoadLines()
: InStr(Arg,"AHK:openconfig") ? RunPath(ahkpath " F:\notebook\prices\Configurator_Scripts\SiemensConfigurator\SiemensFY18Pricing_WithPIA_H.ahk """ doctitle """", "F:\notebook\prices\Configurator_Scripts\SiemensConfigurator\")
: InStr(Arg,"AHK:opennewconfig") ? RunPath(ahkpath " F:\notebook\prices\Configurator_Scripts\SiemensConfigurator2021\SiemensSelector.ahk """ Doctitle """ " ScriptHwnd """", "F:\notebook\prices\Configurator_Scripts\SiemensConfigurator2021\")
: InStr(Arg,"AHK:NoteStamp") ? InsertStamp()
: InStr(Arg,"AHK:convertOrder") ? Order(Arg)
: InStr(Arg,"AHK:fillspr") ? RunPath("F:\userfolders\md\autohotkey\autohotkey.exe F:\notebook\prices\Configurator_Scripts\QuoteForm\PIA_SPR.ahk ""."" . """ doctitle """", "F:\notebook\prices\Configurator_Scripts\QuoteForm\OrderForm")
: ""
}else if (Event.Method = "Inspector.Detached"){
ExitFunc()
}
return
}
EDIT**** Just realized your error is because its case sensitive. ***********
PageInst.Call("Console.enable") will work, while PageInst.call("Console.Enable") WILL NOT.
Thanks
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
@Tre4shunter
That was it!! I knew it had to be something silly I was doing! ugh.. sorry!
And thank you so much for pointing it out!
That was it!! I knew it had to be something silly I was doing! ugh.. sorry!
And thank you so much for pointing it out!
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
How many of you run into following error? while making new chrome instance very first time after starting PC,
(ChromeInst := new Chrome( A_ScriptDir "\ChromeProfile")) and then you reload script and re-create ChromeInst and it work without any issue, its only one time case after starting PC, please tell me coz I really wana know about it...!
So what is going on above?:
As far as I know...... When we create ChromeInst := new Chrome( A_ScriptDir "\ChromeProfile") chrome create 1 process chrome.exe (we can check it in task manager) and after that, chrome try to create New tab by putting few more chrome.exe as processes and we know chrome setting up debugging ip and port ( if you using chrome.ahk you can find it all in there), but very first time chrome takes time (idk why its only for first time), and thats where we/I face above error because chrome haven't created that TAB,
thats only my theory not fact chrome dev know more about it
Everyday I have faced above error, I know I gonna face this error running chrome.ahk when ever I make first ChromeInst after starting PC, idk this happened to y'all or its only with me, So, I have studied it for weeks and came up with a solution which worked,
and you can test it something like this, and I was surprised my above theory was correct
and thats my result
Hope y'all find this useful, and its common sense while WaitforNewChromeProcess if you close any chrome tab (any mean every chrome tab both created with chrome.ahk or User), above code stop gonna wait and you might face error
if anyone having same issue please tell me if WaitforNewChromeProcess worked you too?
I should have named this function something like WaitForChromeProcessChange or wfcpc
(ChromeInst := new Chrome( A_ScriptDir "\ChromeProfile")) and then you reload script and re-create ChromeInst and it work without any issue, its only one time case after starting PC, please tell me coz I really wana know about it...!
So what is going on above?:
As far as I know...... When we create ChromeInst := new Chrome( A_ScriptDir "\ChromeProfile") chrome create 1 process chrome.exe (we can check it in task manager) and after that, chrome try to create New tab by putting few more chrome.exe as processes and we know chrome setting up debugging ip and port ( if you using chrome.ahk you can find it all in there), but very first time chrome takes time (idk why its only for first time), and thats where we/I face above error because chrome haven't created that TAB,
thats only my theory not fact chrome dev know more about it
Everyday I have faced above error, I know I gonna face this error running chrome.ahk when ever I make first ChromeInst after starting PC, idk this happened to y'all or its only with me, So, I have studied it for weeks and came up with a solution which worked,
Code: Select all
ChromeInst := new Chrome( A_ScriptDir "\ChromeProfile")
WaitforNewChromeProcess() ; all this is doing waiting for new TAB to be created by counting chrome.exe processes
Code: Select all
s := ChromeProcessCount()
ChromeInst := new Chrome( A_ScriptDir "\ChromeProfile")
r := WaitforNewChromeProcess()
msgbox, % "Chrome.exe process count:`nBefore creating instance:" s "`n" r
return
WaitforNewChromeProcess(sleep=500)
{
i := n := ChromeProcessCount()
while (n = i)
{
sleep, % sleep
i := ChromeProcessCount()
}
return "after creating instance:" n "`nAfter Creating new Tab:" i
}
ChromeProcessCount()
{
n := 0
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
if( process.Name = "Chrome.exe")
++n
return n
}
Hope y'all find this useful, and its common sense while WaitforNewChromeProcess if you close any chrome tab (any mean every chrome tab both created with chrome.ahk or User), above code stop gonna wait and you might face error
if anyone having same issue please tell me if WaitforNewChromeProcess worked you too?
I should have named this function something like WaitForChromeProcessChange or wfcpc
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
-
- Posts: 139
- Joined: 26 Jan 2016, 16:05
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
I just do this:
Seems to work reliably for me each time - just change 'QuoteFormOriginal' to whatever the document title/tab is in your case. obviously, add timeouts or whatnot in case the tab never does exist, etc...
Thanks!
Code: Select all
ChromeInst := new Chrome(profile,"--app=" Path, "--window-size=1010," Height " --disable-web-security,"",port)
WinActivate, QuoteFormOriginal
While !WinExist("QuoteFormOriginal")
sleep, 100
Thanks!
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
Nice..! who is not familiar with WinExist().... but sadly my web page does not change title and I have more than one chrome instance and every instance has an entirely different task with different sub-url but with same title, you know why did I use approach like this but I can assure ComObjGet("winmgmts:") is damn fast,Tre4shunter wrote: ↑09 Jul 2021, 07:08Seems to work reliably for me each time - just change 'QuoteFormOriginal' to whatever the document title/tab is in your case. obviously, add timeouts or whatnot in case the tab never does exist, etc...
Thanks!
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
-
- Posts: 139
- Joined: 26 Jan 2016, 16:05
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
Are your webpages custom html?
In my case..i have several running too. What I do, is they all start with the same name i.e. "OriginalPage", and there is a function that executes on DOMContentLoaded, which fires a callback to my ahk script -> telling the script that the page is ready to be interacted with. The next action is to change the document.title to a unique identifier for that page i.e. FormPage - %A_TickCount% - which is how I interact with it from that point forward.
Hope that helps!
Thanks
-tre4
In my case..i have several running too. What I do, is they all start with the same name i.e. "OriginalPage", and there is a function that executes on DOMContentLoaded, which fires a callback to my ahk script -> telling the script that the page is ready to be interacted with. The next action is to change the document.title to a unique identifier for that page i.e. FormPage - %A_TickCount% - which is how I interact with it from that point forward.
Hope that helps!
Thanks
-tre4
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
The issue is when I set page title using document.title its do changes the visible name/wintitle, but when I get page title var := document.title or WinExist() it remains unchanged, they use something (called crystal report and saps ) beside js and their HTML is also tricky like I have hundreds of div and every div have same ID having single innertext inside two table #id.table.tbody.tr.td.table.tr.td.innerTextTre4shunter wrote: ↑09 Jul 2021, 09:35Are your webpages custom html?
In my case..i have several running too. What I do, is they all start with the same name i.e. "OriginalPage", and there is a function that executes on DOMContentLoaded, which fires a callback to my ahk script -> telling the script that the page is ready to be interacted with. The next action is to change the document.title to a unique identifier for that page i.e. FormPage - %A_TickCount% - which is how I interact with it from that point forward.
Hope that helps!
Thanks
-tre4
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
I'm just a beginner with javascript, but shouldn't these work?
Code: Select all
;1
js = myFunc(); function myFunc() {const json = '{"result":true, "count":42}'; const obj = JSON.parse(json); return obj.count;};
msgbox % pageInst.Evaluate(js)
;2
js = myFunc(); function myFunc() {const json = '{"result":true, "count":42}'; const obj = JSON.parse(json); return obj;};
ret := pageInst.Evaluate(js)
msgbox % ret["count"]
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
by doing pageInst.Evaluate(js).value;1 is working for me, to make ;2 work, you need to change parameters of Runtime.evaluate specifically parameter returnByValue you need to change it to true like this "returnByValue": Chrome.Jxon_True() in chrome.ahkerohtar wrote: ↑13 Jul 2021, 02:35I'm just a beginner with javascript, but shouldn't these work?
Code: Select all
;1 js = myFunc(); function myFunc() {const json = '{"result":true, "count":42}'; const obj = JSON.parse(json); return obj.count;}; msgbox % pageInst.Evaluate(js) ;2 js = myFunc(); function myFunc() {const json = '{"result":true, "count":42}'; const obj = JSON.parse(json); return obj;}; ret := pageInst.Evaluate(js) msgbox % ret["count"]
read my following post, there I passed a function to read a table and save it as array and using eval_obj() to extract it
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=42890&start=360#p382858
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
@Xeo786 Thank you! That .value correction and your function are very helpful!
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
What is the way to retrieve an object contents that's written to the console?
Say I wanted to retrieve sender and value here.
msgbox % Event.params.message.text ;this gives me [object Object]
Edit: One roundabout way is to do this
and receive the string and then parse/convert it to an AHK object, but that's not ideal.
@Xeo786
FWIW I restarted after several days today, and got that error too that you mentioned above.
Say I wanted to retrieve sender and value here.
Code: Select all
<button onclick="console.log({'sender':'button1', 'value':'clicked'})">Click Me!</button>
Edit: One roundabout way is to do this
Code: Select all
<button onclick="var x = {'sender':'button1', 'value':'clicked'}; console.log(JSON.stringify(x))">Click Me!</button>
@Xeo786
FWIW I restarted after several days today, and got that error too that you mentioned above.
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
You can use element.getAttribute('onclick') and then you can parse the result to extract sender valueerohtar wrote: ↑13 Jul 2021, 09:19What is the way to retrieve an object contents that's written to the console?
Say I wanted to retrieve sender and value here.msgbox % Event.params.message.text ;this gives me [object Object]Code: Select all
<button onclick="console.log({'sender':'button1', 'value':'clicked'})">Click Me!</button>
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
@Xeo786
Hahha that's simple and efficient! Thank you!
Hahha that's simple and efficient! Thank you!
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
I figured out a method to improve the speed of the evaluate function.
When the return value becomes large, it can take some seconds to retrieve the data.
A workarround is to quickly copy the data to the clipboard.
Is this the best way to retrieve data, or is there a better/cleaner/faster way ?
Code: Select all
js =
(
var dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = document.documentElement.outerHTML;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
)
ClipboardSaved := ClipboardAll
Clipboard:=""
ArrElements := page.Evaluate(js).value
ClipWait
result:= Clipboard
Clipboard := ClipboardSaved
ClipboardSaved := ""
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
Use teadrinker`s json parser.
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
Do you mean that it is best to modify the evaluate function of Chrome?
Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!
No, this will not modify evaluate function, but increase speed of chrome response decoding
GeekDude uses Coco json parser which is very slow.
https://www.autohotkey.com/boards/viewtopic.php?p=218916#p218916
GeekDude uses Coco json parser which is very slow.
https://www.autohotkey.com/boards/viewtopic.php?p=218916#p218916