Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Submit form with DOM


Best Answer Jackie Sztuk _Blackholyman , 14 July 2013 - 09:48 PM

Go to the full post »


  • Please log in to reply
9 replies to this topic
mpolenis
  • Members
  • 7 posts
  • Last active: Nov 19 2014 08:44 PM
  • Joined: 09 Jul 2013

I am struggling to submit a form using COM with username, password, a submit and a reset button.

I want this to work while IE is hidden.

 

html source for the submit button is

<input type="submit" value="Login" name="submit" onClick="return checkInput();">

 

the code that I try is

wd.document.getElementsByTagName("input").submit.click()

which it does not work whether IE is hidden or visible

 

When I try this in IE console (IE visible of course) works fine.

What am I doing wrong?

 

The form itself has also a POST method and maybe someone help me with this if this is better way to do it

<form enctype="multipart/form-data" method="post" name="user_auth_form" action="" onsubmit="process(this)">

How can I trigger the onsubmit action of that form (sorry if this have been already answered)



rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
Maybe try

wd.document.all.submit.click()

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


mpolenis
  • Members
  • 7 posts
  • Last active: Nov 19 2014 08:44 PM
  • Joined: 09 Jul 2013

Thank you for your answer but this does not work either

 

Actually if I enable warnings (#Warn), I get this warning:

 

---------------------------
KeepAlive1.33.ahk
---------------------------
Warning:  This variable has not been assigned a value.

Specifically: wd  (a global variable)

    Line#
    152: wb.document.user_auth_form.fw_username.value := Usn  
    153: Sleep,100
    154: wb.document.user_auth_form.fw_Password.value := Pwd  
    155: Sleep,100
--->    157: wd.document.all.submit.click()  
    158: }
 

which by the way is the same with both implementation

wd.document.getElementsByTagName("input").submit.click()

and

wd.document.all.submit.click()

but I suppose this is not critical.

 

Thank you any way, I look forward for another Idea.



Rijul Ahuja
  • Members
  • 763 posts
  • Last active: Nov 25 2013 10:16 AM
  • Joined: 14 Mar 2012

In your OP, you had suggested that the ComObject was named wd, hence rbrtryn suggested you use wd. For your case, you should use

wb.document.all.submit.click()

Abandon the forum. The community has decided in a democratic vote to leave this website because of inactive and perverse administration.

Very few of the contributing members remain here.


mpolenis
  • Members
  • 7 posts
  • Last active: Nov 19 2014 08:44 PM
  • Joined: 09 Jul 2013

As I wrote in my previous post I tried this also.

 

wb.document.all.submit.click() does not work inside AHK script for this particular page.

 

The wired thing is that it works when I send it through IE console.



rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
Post more of the code then, we don't have enough information to work with

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


mpolenis
  • Members
  • 7 posts
  • Last active: Nov 19 2014 08:44 PM
  • Joined: 09 Jul 2013

Thank you for staying with me I tried to figure out this for at least two days now.

Apologize to programmers out there for the code but here it is:

 

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

F8::
gosub Reconnect
return

Reconnect:

IniRead, Usn, KeepAlive, Credentials, MyUserName        ; Get UserName from external file
IniRead,Pwd, KeepAlive, Credentials, MyPassword         ; Get Password grom external file

If Usn = ERROR Or Pwd = ERROR Or Usn = "" Or Pwd = ""     ; Find a better way
    {
;    gosub, WriteErrorReport                                ; Report the error
;    gosub, inierror                                        
    }

;SetTitleMatchMode RegEx

wb := ComObjCreate("InternetExplorer.Application")                      ; Create an IE object

;wb.Visible := true                                                      ; Make the IE object visible

wb.Navigate("https:\\192.168.51.254:4100")                              ; Connect to authentication server

While wb.readyState!=4 || wb.document.readyState!="complete" || wb.busy ; Wait to load
   Sleep 100
   
txt := wb.document.getElementsByTagName("title")[0].innerText

pos := instr(txt, "Σφάλμα πιστοποιητικού") + InStr(txt, "Certificate error")       ;Find better way so it does not depends on Locals

If pos > 0                                ; Found!
{
 wb.document.getElementById("overridelink").click()                          ; Override default IE Certificate Error page
}
While wb.readyState!=4 || wb.document.readyState!="complete" || wb.busy     ; Wait to Load
   Sleep 100
   
txt := wb.document.getElementsByTagName("title")[0].innerText
pos := instr(txt, "User Authentication")                                    ;Find a better way

if pos > 0
{
wb.document.user_auth_form.fw_username.value := Usn                            ; Enter User Name
sleep, 100
wb.document.user_auth_form.fw_Password.value := Pwd                            ; Enter Password
sleep, 100
;wd.document.all.form.submit()                                                                    ; Does not work
wd.document.getElementsByTagName("input").click()                                                ; Does not work
;wd.document.all.submit.click()                                                                    ; Does not work
;wb.document.parentWindow.execScript(wd.document.getElementsByTagName("input").click())            ; javascript I think
}

wb.Visible := true
;~ sleep 5000
;~ send {Enter}                                                                                    ; This WORKS! But with IE Visible

While wb.readyState!=4 || wb.document.readyState!="complete" || wb.busy ; Wait for the site to load
   Sleep 100
Sleep 1000

txt := wb.document.getElementsByTagName("title")[0].innerText

Sleep 3000
wb.Quit()                                ; Close IE It does not always work

pos := instr(txt, "User Authenticated")

  if (pos > 0) {
      TrayTip, Σύνδεση επιτυχής!, Είστε συνδεδεμένοι στο διαδίκτυο. , , 1
       Sleep, 1000
  }
 else {
      TrayTip, Πρόβλημα σύνδεσης!, Δυστυχώς το σύστημα δεν κατάφερε να σας πιστοποιήσει.`nΒεβαιωθείτε ότι τα διαπιστευτήρια σας είναι σωστά`nή ότι δεν χρησιμποιούνται από άλλο χρήστη.`nΘα γίνει ξανά προσπάθεια επανασύνδεσης. , , 3
      Sleep, 5000
  }

return

 

; END

 

 

Here is the source of the page ("https:\\192.168.51.254:4100") to see for your self.

You may also suggest another way to accomplish this like javascript (which I now try to figure out) so I can trigger the appropriate function directly (which one?)

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...ransitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="expires" content="-1">
<link rel="StyleSheet" type="text/css" href="/style/styles.css">
<link rel="StyleSheet" type="text/css" href="/style/fonts.css">
<script language="JavaScript" src="/scripts/general.js"></script><script language="JavaScript" src="/scripts/cookies.js"></script><title>User Authentication</title>
<script>
          var cgi_action = "fw_logon";
          var success_xsl = "fw_logon_success.xsl";
          
           cgi_action = 'fw_logon';
          
          if (cgi_action == "sslvpn_logon") {
            success_xsl = "sslvpn_logon_success.xsl";
          }
          
          function positionSelect(obj, selectValue)
          {
          if(obj==null) return;
          if(obj.options==null) return;
          if(obj.options.length==0) return;
          for(var i=0; i < obj.options.length; i++)
          {
          if(obj.options[i].value==selectValue)
          {
          obj.selectedIndex=i;
          return;
          }
          }
          obj.selectedIndex=0;
          }

          function getSelectValue(obj)
          {
          if(obj==null) {
//          alert('getSelectValue():Object is null!');
          return null;
          }
          if(obj.options==null) {
//          alert('getSelectValue():Object "'+obj.name+'" is not select object!');
          return null;
          }
          if(obj.options.length==0) {
//          alert('getSelectValue():Object "'+obj.name+'" is empty!');
          return null;
          }
          return obj.options[obj.selectedIndex].value
          }

          function get_cookie ( cookie_name )
          {
          var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

          if ( results )
          return ( unescape ( results[1] ) );
          else
          return null;
          }

          function set_cookie2(searchname, value, expirelapse, path)
          {
          if((expirelapse==null)||(expirelapse=="null")) { expirelapse=365*24*60*60*1000;}

          var dc = document.cookie;

          var expires=new Date();
          expires.setTime(expires.getTime() + expirelapse);

          var curCookie = searchname + "=" + value
          + "; expires=" + expires.toGMTString() + ";";

          if ( path ) curCookie += "; path=" + escape ( path );

          document.cookie = curCookie;
          }

          function process(form)
          {
          set_cookie2("domain", getSelectValue(form.fw_domain) );
          set_cookie2("username", form.fw_username.value);
          }

          function gather(form)
          {
          var domain   = get_cookie("domain");
          var username = get_cookie("username");
          positionSelect(form.fw_domain, domain?domain:"");
          form.fw_username.value= username?username:"";
          }
          function checkInput()
          {
          if(document.user_auth_form.fw_username.value.length==0){
          alert('Please enter your username!');
          return false;
          }
          if(document.user_auth_form.fw_password.value.length==0){
          alert('Please enter your password!');
          return false;
          }
          }
        </script>
</head>
<body style="background-color:#FFFFFF" onload="gather(document.user_auth_form)"><form enctype="multipart/form-data" method="post" name="user_auth_form" action="" onsubmit="process(this)">
<table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">
<tr><td><img src="/images/pixel.gif" width="1" height="75" border="0" alt=""></td></tr>
<tr align="center" valign="middle"><td><table border="0" align="center" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img src="/images/pixel.gif" width="1" height="21" border="0" alt=""></td>
<td colspan="2" style="background-repeat: repeat-x; height: 21px;"></td>
<td><img src="/images/pixel.gif" width="1" height="21" border="0" alt=""></td>
</tr>
<tr>
<td><img src="/images/pixel.gif" width="1" height="40" border="0" alt=""></td>
<td colspan="2" bgColor="#993333" width="570" height="40"><img src="/images/pixel.gif" width="570" height="40" border="0" alt=""></td>
<td><img src="/images/pixel.gif" width="1" height="40" border="0" alt=""></td>
</tr>
<tr>
<td width="50%"><img src="/images/pixel.gif" width="1" height="1" border="0" alt=""></td>
<td colspan="2" bgcolor="#ffffff" style="border-right: 1px solid #666666; border-left: 1px solid #666666;"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>
<td style="border-right: 1px solid #666666;padding: 0px 5px 0px 5px" align="center" width="180"><img src="/images/login_logo.gif" border="0" alt="WatchGuard Technologies, Inc."></td>
<td align="left" style="padding-left: 30px"><table border="0" cellpadding="0" cellspacing="0">
<tr><td colspan="2"><img src="/images/pixel.gif" width="1" height="30" border="0" alt=""></td></tr>
<tr>
<td align="right"><b>Username: </b></td>
<td><input type="text" name="fw_username" size="20" value="username" autocapitalize="off"></td>
</tr>
<tr><td colspan="2"><img src="/images/pixel.gif" width="1" height="5" border="0" alt=""></td></tr>
<tr>
<td align="right"><b>Password: </b></td>
<td><input type="password" name="fw_password" size="20" value=""></td>
</tr>
<tr><td colspan="2"><img src="/images/pixel.gif" width="1" height="5" border="0" alt=""></td></tr>
<tr style="display:none">
<td align="right"><b>Domain: </b></td>
<td><select name="fw_domain" class="formtext"><option value="Firebox-DB">Firebox-DB</option></select></td>
</tr>
<tr style="display:none">
<input type="hidden" name="fw_domain" value="Firebox-DB"><td colspan="2"><img src="/images/pixel.gif" width="1" height="5" border="0" alt=""></td>
</tr>
<tr>
<td><img src="/images/pixel.gif" width="1" height="10" border="0" alt=""></td>
<td><div align="right">
<img src="/images/pixel.gif" width="1" height="20" border="0" alt=""><input type="submit" value="Login" name="submit" onClick="return checkInput();"><input type="reset" value="Reset" name="Reset">
</div></td>
</tr>
<tr id="needCookie" style="display:none;"><td colspan="2" style="color:red;"><a href="#" onClick='JavaScript:alert("[TODO] To launch help where we instruct the users to enable Cookies"); return false'></a></td></tr>
<tr><td colspan="2"><img src="/images/pixel.gif" width="1" height="15" border="0" alt=""></td></tr>
</table></td>
</tr></table></td>
<td width="50%"><img src="/images/pixel.gif" width="1" height="1" border="0" alt=""></td>
</tr>
<tr>
<td><img src="/images/pixel.gif" width="1" height="40" border="0" alt=""></td>
<td colspan="2" bgColor="#993333" width="570" height="40"><img src="/images/pixel.gif" width="570" height="40" border="0" alt=""></td>
<td><img src="/images/pixel.gif" width="1" height="40" border="0" alt=""></td>
</tr>
</table></td></tr>
<tr><td><img src="/images/pixel.gif" width="1" height="75" border="0" alt=""></td></tr>
</table>
<input type="hidden" name="action" value="fw_logon"><input type="hidden" name="style" value="fw_logon_progress.xsl"><input type="hidden" name="fw_logon_type" value="logon"><script language="JavaScript">document.user_auth_form.fw_username.focus();</script>
</form></body>
</html>



mpolenis
  • Members
  • 7 posts
  • Last active: Nov 19 2014 08:44 PM
  • Joined: 09 Jul 2013

One last try before I quit. (I am not an easy quitter but this is beyond me. Trying for 3 full days now).

To anyone who can answer:

 

Why a command that executed from the console of IE does not execute through the COM interface of AHK?

Specifically when I try document.all.submit.click() through the IE console execute,

but if I try wd.document.all.submit.click() through AHK does not execute (where wd the IE instance of course).

Even more I can set the focus through the AHK with wd.document.all.submit.focus().

 

I am not a programmer and I have not intentions to be one.

I just wanted to accomplish this simple task of automating this login procedure.

 

Thank you for your help.



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
✓  Best Answer
both this
wb.document.all.submit.click()
And this
loop % (Inputs := wb.document.getElementsByTagName("input")).length
    try
        if (Inputs[A_index-1].name = "submit")
            Inputs[A_index-1].click()
works for me with the html you posted

so im not sure where the problem is other then you seem to keep going from wb to wd and back to wb in you script so try and make sure all the pointers are the same name

Also try this
Form := wb.document.all.user_auth_form
Form.submit()
Or this may work
IID := "{332C4427-26CB-11D0-B483-00C04FD90119}", SID := IID
window := ComObj(9,ComObjQuery(wb, SID, IID),1)
Form := wb.document.all.user_auth_form
msgbox % form.innerhtml
if not window.checkInput()
    return
else
    window.process(form)

Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

mpolenis
  • Members
  • 7 posts
  • Last active: Nov 19 2014 08:44 PM
  • Joined: 09 Jul 2013

Blackholyman, finally you pointed out my ignorance (how stupid am I? I spent three days not seen the difference between wb and wd. Oh! I am embarrassed).

 

My sincerely apologies to all out there who spent his/her time looking my silly posts, especially to Riijul Ahuja who first pointed out my mistake

and of course to rbrtryn (at least I double check the spelling of your nick names).

 

To be optimistic though I have to admit that I learned a lot about html, forms, methods etc.

 

Thank you so much.