EddNewGate
EddNewGate

Reputation: 537

AutoHotKey, set focus on a button webpage and send click

I have one button on my html page with window title "Page":

<button onclick="alert('This button has been clicked')">Submit</button>

and using AutoHotKey, I'm trying to set focus on him and then send a mouse click.

This is the AHK code i write:

^p::
ControlFocus, Submit, Page
MouseClick left
return

On pressing Ctrl+P keys, it should do his job. Unfortunately, it doesn't work. I've read the documentation with some examples and I can't get it to work...

Upvotes: 2

Views: 10244

Answers (1)

Joe DF
Joe DF

Reputation: 5568

You can learn a lot more about intergrating AHK with HTML DOM here:
https://autohotkey.com/boards/viewtopic.php?f=7&t=4588
See the following example on how something like this can be achieved.

Example

#SingleInstance, off
OnExit,OnExit

Gui Add, ActiveX, x0 y0 w640 h480 vWB, Shell.Explorer  ; The final parameter is the name of the ActiveX component.
WB.silent := true ;Surpress JS Error boxes
WB.navigate("http://example.com/") ;put your web address here...
ComObjConnect(WB, WB_events)  ; Connect WB's events to the WB_events class object.
Gui Show, w640 h480

return

GuiClose:
OnExit:
ExitApp

class WB_events
{
    ;NavigateComplete2(wb, NewURL)
    ;DownloadComplete(wb, NewURL)
    DocumentComplete(wb, NewURL)
    {
        if (WB.ReadyState == 4) { ; see http://stackoverflow.com/a/9835755/883015
            Sleep, 300 ;wait 300 ms
            
            ;Do what you here...
            ;for example, click the first link on the page, you can change this for a button.
            wb.document.getElementsByTagName("a")[0].click()
            MsgBox Item was clicked.
        }
        return
    }
}

Upvotes: 1

Related Questions