Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I populate fields on a webpage...

Status
Not open for further replies.

VisualGuy

Programmer
May 27, 2003
162
0
0
US
I'm trying to write a program that will not only launch a page through IE, but it will populate the userid and password fields and hit enter. The code I have so far is as follows:

Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate (" IE.Visible = True
Set IE = Nothing

This works to bring up the website, but this is as far as I could get. I tried using sendkeys and I can't get that to work at all. If you had a userid field, password field and a login button, how would you automate this? Any ideas?
 
VisualGuy,

What you need to do is look at the elements on the page by either their name or id. Then send that element/object a value of username and password for each respectively. Then once you have sent those, do a fire event to submit the page.

Below I have enclosed a snippet of my Send Html Function which enumerates through the elements on a webpage looking for either the "I" ID or "N" for the name and then passes a value to that field, textarea, or radio/checkbox.

So if my html tag was:

<INPUT TYPE=&quot;password&quot; Name=&quot;ACCESS1&quot; ID=&quot;ACCESS2&quot; Value=&quot;&quot;>

I would call the following to look at this tag
by ID:
SendHInfo(&quot;ACCESS2&quot;, &quot;INPUT&quot;, &quot;I&quot;, &quot;MyPASSWORD&quot;)
by NAME:
SendHInfo(&quot;ACCESS1&quot;, &quot;INPUT&quot;, &quot;N&quot;, &quot;MyPASSWORD&quot;)

The above would place the text &quot;MyPASSWORD&quot; into the above tagline element.

To use the following, make sure to get ahold of the IE document, so you can reference it.

------------------------------
Private Idoc as HTMLDocument
Set Idoc = IE.Document
------------------------------

Then call the function:



------------------------------
Public Function SendHInfo(stFIELD, stTAG, stID_NAME, stVALUE)

On Error Resume Next

' Ex: SendHInput(&quot;keyword&quot;, &quot;input&quot;, &quot;N&quot;, value)
' It will locate the type of field (stTAG) by calling
' that collection of objects from that page. It will then loop
' Through all of those objects looking at the ID (OR) Name properties
' as defined by (stId_Name) and verify that the destination object
' equals (stField).


If UCase(stID_NAME) = &quot;I&quot; Or UCase(stID_NAME) = &quot;N&quot; Then

Set colInputElements = iDoc.getElementsByTagName(stTAG)


If UCase(stID_NAME) = &quot;I&quot; Then ' Look at ID's
For Each objelement In colInputElements
If InStr(1, UCase(objelement.Id), UCase(stFIELD)) > 0 Then
' Found Field Set the Value
If UCase(objelement.Type) = &quot;CHECKBOX&quot; Or UCase(objelement.Type) = &quot;RADIO&quot; Then
If stVALUE = -1 Or stVALUE = 0 Then
' Set specific Value
If UCase(objelement.Checked) = &quot;TRUE&quot; Then
If stVALUE = 0 Then
objelement.Checked = 0
End If
Exit Function
End If
If UCase(objelement.Checked) = &quot;FALSE&quot; Then
If stVALUE = -1 Then
objelement.Checked = &quot;True&quot;
End If
Exit Function
End If
Exit Function
End If

' Swap Checked Value
If objelement.Checked = &quot;True&quot; Then
objelement.Checked = 0
Exit Function
Else
objelement.Checked = &quot;True&quot;
Exit Function
End If
Else

' Set Input Value (text area, text box, select ect...)
objelement.Value = stVALUE
End If
Exit Function
End If
Next
End If


If UCase(stID_NAME) = &quot;N&quot; Then ' Look at NAMES
For Each objelement In colInputElements
If InStr(1, UCase(objelement.Name), UCase(stFIELD)) > 0 Then
' Found Field Set the Value
If UCase(objelement.Type) = &quot;CHECKBOX&quot; Or UCase(objelement.Type) = &quot;RADIO&quot; Then
If stVALUE = -1 Or stVALUE = 0 Then
' Set specific Value
If UCase(objelement.Checked) = &quot;TRUE&quot; Then
If stVALUE = 0 Then
objelement.Checked = 0
End If
Exit Function
End If
If UCase(objelement.Checked) = &quot;FALSE&quot; Then
If stVALUE = -1 Then
objelement.Checked = &quot;True&quot;
End If
Exit Function
End If
Exit Function
End If

' Swap Checked Value
If objelement.Checked = &quot;True&quot; Then
objelement.Checked = 0
Exit Function
Else
objelement.Checked = &quot;True&quot;
Exit Function
End If
Else

' Set Input Value (text area, text box, select ect...)
objelement.Value = stVALUE
End If
Exit For
End If
Next
End If
End If

End Function

------------------------------

Rock6431.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top