Help with AHK and COM

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cybergg
Posts: 7
Joined: 27 Dec 2019, 04:31

Help with AHK and COM

27 Dec 2019, 04:48

Hey guys,

I've followed the tutorial on how to control websites using COM on this thread: https://autohotkey.com/board/topic/64563-basic-ahk-v11-com-tutorial-for-webpages/

When executing my .ahk file I get an error as soon the websites open and the script tries to click on the button to search on google.

Code: Select all

IELoad(wb)    ;You need to send the IE handle to the function unless you define it as global.
{
    If !wb    ;If wb is not a valid pointer then quit
        Return False
    Loop    ;Otherwise sleep for .1 seconds untill the page starts loading
        Sleep,100
    Until (wb.busy)
    Loop    ;Once it starts loading wait until completes
        Sleep,100
    Until (!wb.busy)
    Loop    ;optional check to wait for the page to completely load
        Sleep,100
    Until (wb.Document.Readystate = "Complete")
Return True
}

wb := ComObjCreate("InternetExplorer.Application") ;create an IE instance
wb.Visible := True
wb.Navigate("Google.com")
IELoad(wb)
wb.Document.All.q.Value := "site:autohotkey.com tutorial"
wb.Document.All.btnK.Click()
IELoad(wb)
I already tried to click throug element by id / element by name.
ahk.PNG
(15.12 KiB) Downloaded 165 times
AHK Version: 1.1.24.02 (can't update to newer version because of managed desktop client)
OS: Windows 10

I hope you guys can help me!
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: Help with AHK and COM

27 Dec 2019, 08:00

The code from this tutorial might be outdated (especially the document.all syntax).

This works - but there are different ways to identify the right webpage elements... I used the class names.

Code: Select all

wb := ComObjCreate("InternetExplorer.Application") ;create an IE instance
wb.Visible := True
wb.Navigate("https://www.google.de/")
IELoad(wb)
wb.document.getElementsByClassName("gLFyf gsfi")[0].value := "site:autohotkey.com tutorial"
msgbox weiter
wb.document.getElementsByClassName("gNO89b")[0].click()
IELoad(wb)


IELoad(wb)    ;You need to send the IE handle to the function unless you define it as global.
{
    If !wb    ;If wb is not a valid pointer then quit
        Return False
    Loop    ;Otherwise sleep for .1 seconds untill the page starts loading
        Sleep,100
    Until (wb.busy)
    Loop    ;Once it starts loading wait until completes
        Sleep,100
    Until (!wb.busy)
    Loop    ;optional check to wait for the page to completely load
        Sleep,100
    Until (wb.Document.Readystate = "Complete")
Return True
}
(Have a look at the google page source code to see where the class names come from.)
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Help with AHK and COM

27 Dec 2019, 08:12

Inside your IELoad function I would do the following

Code: Select all

While wb.readyState != 4 || wb.document.readyState != "complete" || wb.busy ; wait for the page to load
   sleep 100
this will make sure that it waits correctly (also is the new / cleaner way to do it.
Also looking at the elements inside of google.com reveals that what you think is a button is not a button. Its actually and input of the following

Code: Select all

<input class="gNO89b" value="Google Search" aria-label="Google Search" name="btnK" type="submit" data-ved="0ahUKEwiKrbLS8dXmAhVCLK0KHa4lDI8Q4dUDCAw">
So in the case of document all we can't use this. Instead lets use a class selector from javascript.

Code: Select all

wb.document.getElementsByClassName("gNO89b")[0].click()
This will allow us to click that input and trigger the search.
PS. This code is similar to @gregster but I used an updated while loop for the function

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, macromint, peter_ahk and 324 guests