-3

I am trying to use VBA to automate input of Username/Password on a form that doesn't have any ID or Name to it. Is this possible?

Have a look at the sample webpage. This webpage doesn't work with the names from what I can tell.

VBS code:

uname = "myusername"
pword = "mypassword"

Set objIE = CreateObject("InternetExplorer.Application")
WebSite = "https://cis.mesaaz.gov/eservices/p_template"
With objIE
  .Visible = True
  .Navigate WebSite
    Do While .Busy Or .ReadyState <> 4
        DoEvents
    Loop

    Set element = .Document.getElementsByName("userid")
    element.Item(0).Value = uname

    Set element = .Document.getElementsByName("ping")
    element.Item(0).Value = pword

    Set element = .Document.getElementsByName("submit")(0)
    element.Click

    Do While .Busy Or .ReadyState <> 4
        DoEvents
    Loop

End With

Form HTML code:

<form action="uwpqutil.p_VALLOGIN" method="POST" target="_top" name="Login">
<center>
<table border="0" cellpadding="3" cellspacing="0">
<tbody><tr>
<td rowspan="1" colspan="3" align="LEFT"><b>Log-In</b></td>
</tr>
<tr>
<td valign="middle"><b>UserName</b></td>
<td valign="middle"><input name="userid" size="30" type="text"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td valign="middle"><b>Password</b></td>
<td valign="middle"><input name="pin" size="30" type="password"></td>
<input name="JavaScript" value="enabled" type="hidden">
<script language="JavaScript"><!--
document.Login.JavaScript.value = 'enabled';
//--></script>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td valign="middle" align="right"><input value="Log-In" type="submit"></td>
</tr>
</tbody></table>
</center>
</form>
Serenity
  • 35,289
  • 20
  • 120
  • 115
user3809423
  • 13
  • 1
  • 4

1 Answers1

1

Errors seen right away

From what I can see immediately, you are getting input named ping instead of pin and .Document.getElementsByName("submit") returns no elements because there is no element with name submit.

You could instead use getElementsByTagName("input") and loop through the result to find the correct one, or you can get the form itself via getElementsByName("Login") and use its Submit method.

Also there is no DoEvents function in VBS. Instead, you should use WScript.Sleep with reasonable number of milliseconds as argument (I suggest 100). (Assuming WScript object is available. There are circumstances it is not.)

Deeper-rooted error

However, there is also a more fundamental error in your code. The page you request does not contain the form. It contains only frames definition. If you change WebSite to

WebSite = "https://cis.mesaaz.gov/eservices/p_UWGPDYNA"

it starts working, but does not load other frames.

uname = "myusername"
pword = "mypassword"

Set objIE = CreateObject("InternetExplorer.Application")
WebSite = "https://cis.mesaaz.gov/eservices/p_UWGPDYNA"
With objIE
    .Visible = True
    .Navigate WebSite
    Do While .Busy Or .ReadyState <> 4
        WScript.Sleep 100
    Loop

    With .Document
        .getElementsByName("userid")(0).Value = uname
        .getElementsByName("pin")(0).Value = pword
        .getElementsByName("Login")(0).Submit
    End With

    Do While .Busy Or .ReadyState <> 4
        WScript.Sleep 100
    Loop
End With

If you want to load them too, you have to keep the original URL, select the correct frame and work on its ContentDocument. The code I finished with:

uname = "myusername"
pword = "mypassword"

Set objIE = CreateObject("InternetExplorer.Application")
WebSite = "https://cis.mesaaz.gov/eservices/p_template"
With objIE
    .Visible = True
    .Navigate WebSite
    Do While .Busy Or .ReadyState <> 4
        WScript.Sleep 100
    Loop

    With .Document.getElementsByName("main")(0).ContentDocument
        .getElementsByName("userid")(0).Value = uname
        .getElementsByName("pin")(0).Value = pword
        .getElementsByName("Login")(0).Submit
    End With

    Do While .Busy Or .ReadyState <> 4
        WScript.Sleep 100
    Loop
End With

Non-related errors

You should improve your HTML markup and fix JavaScript detection.

Now the JavaScript input always contains enabled. Its default value should be disabled and it should be changed to enabled by JavaScript.

The markup itself should be at least stripped from elements and attributes taking care of formatting. This should be done in CSS. The style you are using is from the ’90s when CSS has not been around. Also you should validate you markup (it matters). The head is repeated after the body. The frame definition document has script before html element, it should be in head. Errors like that may make your pages behave strangely.

Community
  • 1
  • 1
Palec
  • 12,743
  • 8
  • 69
  • 138
  • Thank you very much! That worked :) Didn't realize I had to talk directly to the frame! – user3809423 Jul 08 '14 at 04:27
  • @user3809423 You’re welcome. But the script originally stopped before reaching the code playing with the document; even if you did realize that you work on the wrong document, the `DoEvents` error would still be a problem. Try running the script from `cmd.exe` via `cscript script.vbs` and you’ll see when an error occurs sometimes. SOmetimes this would need some manual error-handling in the code (see `On Error` statement). I’m not familiar enough with VBS to tell when errors just silently terminate the script. – Palec Jul 08 '14 at 09:37