2
votes

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...

1
HTML elements in rendered pages are far from being represented as own window controls in modern browsers. That is, your browser won't expose a control for every element there is; in fact, it will only expose one control for the entire rendered page or even just one control for the whole browser window. If you're using IE, try COM object automation. In Chrome or Firefox, I recommend writing a userscript.MCL
Except using click coordinates or Tab key, Is there no other way to get the focus on a button ?EddNewGate
@EddNewGate Yes, it is easy so long as you use IE and do what @MCL said about COM object automation. It ends up being something like wb.Document.getElementByID("ElementName").click() (if you know the element name) or wb.document.getElementsByTagName("button")[1].click() if it can only be accessed by its tag type and index. See also autohotkey.com/board/topic/…PGilm
Sorry, you did want to click, right? If all you want is to focus on a control, change the .click() to .focus(). Or, if you need to check the text of the control (like when the button is randomly placed on left or right), you can look at the .innerText or .innerHTML but this is all explained in the two tutorials @MCL and I directed you to.PGilm
@PGilm I don't want to resort to IE. Was searching these days if is possible to make it work, like how it works in IE on other browsers aswell, but later I found out is not possible or not very easy... I know there are other ways to focus a button and then send a click, which works on all browsers, but is not that briliant ( like seting the mouse cursor to specific coordinates on screen which matches the button position). I think I will follow MCL solution provided. I believe if I can write a script to assign hotkeys to a button on a webpage, I can then invoke it in AHK. I'll see :)EddNewGate

1 Answers

1
votes

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