How to click a button using VBScript in an HTA file

Cocoa Dev picture Cocoa Dev · Jun 7, 2012 · Viewed 8k times · Source

My code looks like

<html>
<head>
<title>My HTML application</title>
<HTA:APPLICATION 
    id="frames" 
    border="thin" 
    caption="yes" 
    icon="http://www.google.com/favicon.ico" 
    showintaskbar="yes" 
    singleinstance="yes" 
    sysmenu="yes" 
    navigable="yes" 
    contextmenu="no" 
    innerborder="no" 
    scroll="auto" 
    scrollflat="yes" 
    selection="yes" 
    windowstate="normal" />
</head>

<script language="VBScript">

Sub Window_OnLoad
  'This method will be called when the application loads
  'Add your code here
  GetUserName
End Sub

Sub GetUserName() 
    Set objNetwork = CreateObject("WScript.Network")
    linkTo("http://servername/nph-psf.exe?HOSTID=AD&ALIAS=" & objNetwork.UserName)
    Set objNetwork = Nothing 
End Sub

Sub linkTo(strLink) 
    Document.getElementById("psyncLink").src = strLink
End Sub

Sub checkInputPage
    Document.theforms.name
End Sub

</script>        
    </head> 
    <frameset rows="60px, *"> 
        <frame src="topo.htm" name="topo" id="topo" application="yes" /> 
        <frame src="http://servername/nph-psf.exe?HOSTID=AD&ALIAS=" name="conteudo" id="psyncLink" application="yes" /> 
    </frameset> 
</html> 

and the page loads with 2 buttons, how do I perform the click operation programatically?

<INPUT border=0 type=image alt="Use a password" name="SUBMIT-password.pss" src="docs/pics/en-us/useapassword_button.jpg">

Please note that I can not change any code thats loaded from because I do not have write privs to that directory. I am creating an HTA script that can programatically click buttons and links on that page to assist our users with changing their password

Answer

Cocoa Dev picture Cocoa Dev · Jun 12, 2012

See How to click a button using VBScript in an HTA file

This does exactly what I needed

<script type="text/javascript"> 
function doClick(fr) { 
    var btn = fr.contentWindow.document.getElementsByName("SUBMIT-password.pss"); 
    if (btn.length == 0) { 
        alert("no button!"); 
        return; 
    } else { 
        btn[0].click(); 
    } 
} 
</script>