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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vbscript - How to fill out IE form? 1

Status
Not open for further replies.

hayesb2

Programmer
Jul 22, 2002
19
US
Can anyone provide a sample vbscript which would be used to popoulate a text box in IE and select an option from a drop-down box?
(Reason being is I need to fill out lots of forms and do not know how to code a .vbs file to make IE active and populate...A simple example should point me in the right direction though)
Thanks.
 
Code:
Set oIE = CreateObject("InternetExplorer.application")
With oIE
    .Visible = True
    .navigate ("mail.yahoo.com")
End With
Do Until oIE.ReadyState = 4  'readystate 4 = done loading
    wscript.sleep 200
Loop
With oIE.Document.All
    'be sure your input boxes are named like: <input type=text name="login">
    .Item("login").Value = "Tek-Tips rocks" 'Set username
    .Item("passwd").Value = "fakepass" 'Set password
    MsgBox "Look at the yahoo mail page that was just opened. Then click OK to have script submit user/pass it entered"
    .Item(".save").Click 'Click Submit
End With
 
Forgot to code in the dropdown box. Can probably set the value of it just like the input boxes but I'll test it out and let you know.
 
Danp129,
Thank you!! Thats exactly what I needed to get my start! Let me know if you get the drop-down/radio button to populate...

 
.Value isn't the right method for radio/drop-down selection...
Sorry I am new to VBscript and not familiar with all the property/methods. Below is the error-


Object doesn't support this property or method: 'Item(...).Value'
 
Try .Checked

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
radio buttons have the same name for the option they're controlling. Do you plan to always choose a specific one? If so what is it's name and the order it appears in the source code to the other radios?
 
for the drop down, I was able to use .value and didn't get an error. If I did .Item("dropdown1").Value = "2" then it changed the selection to "b" in the list for me.

Code:
<select name="Dropdown1" value="">
					      <option value="1" Selected>a</option>
					      <option value="2">b</option>
					      <option value="3">c</option>
					      <option value="4">d</option>
					      <option value="5">e</option>
					      <option value="6">f</option></select>Drop 

Down
 
I can get the textbox and drop-down to work, but cannot get the radio or checkbox selections to work. Please see the code I'm trying to work with below -

RADIO

<input type="RADIO" name="prefix" value="Mr" ></font></font>
<font size="1">Mr.</font></b>
<font size="1"></font><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
<font size="1"><input type="RADIO" name="prefix" value="Mrs" >
</font></font><font size="1"><b>Mrs.</b></font><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><font size="1">
<input type="RADIO" name="prefix" value="Miss" >
</font></font><font size="1"><b>Miss</b> </font><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><font size="1">
<input type="RADIO" name="prefix" value="Ms" >


CHECKBOX

<input name="offers" type="checkbox" id="offers" value="yes">
<font face="Verdana, Arial, Helvetica, sans-serif" color="#000000"></font></b></font></td>
 
How could a radio have a value of "Mr."? If you want it to be selecetd, try checked=True.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Code:
.Item("chkRememberMe").Click

Still working on the radios
 
How could a radio have a value of "Mr."? If you want it to be selecetd, try checked=True.

I wondered the same thing, but thats the code I have to work with...

-----------------------------------------------------------
Danp129,

Got the checkbox to work...
 
Did you try .checked?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I tried this -

.Item("prefix").Checked = True

But gives error that the Object doesnt support that method.


 
Tom, the problem with radio buttons is that you cannot refer to it by it's unique name because all the other radio buttons for choosing the various prefixes have the same name. Basically, "prefix" is not a unique name in the "item" collection....

So, I'm making a function to loop through the items...

 
I don't know that you will be able to get the info. There is no unique id for each radio.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Not familiar with vbscript so I may have to struggle a bit, but it will be a fuction similar to this (which isn't working yet):
Code:
Function SelectRadioByValue(oDocument As Object, sRadioValue$) As Boolean
    With oDocument.All
        On Error Resume Next
        For i = 0 To (.Length - 1)
            If .Item(i).Type = "radio" And .itme(i).Value = sRadioValue Then
                On Error GoTo 0
                .Item(i).Click
                SelectRadioByValue = True
                Exit Function
            End If
        Next
        On Error GoTo 0
    End With
    SelectRadioByValue = False
End Function

It will be called like this:
Code:
    If SelectRadioByValue(.Item, "Mr") = True Then
        MsgBox "Woohoo"
    Else
        MsgBox "bummer"
    End If
 
What isn't working about it? Here are some changes that I would try:
Function SelectRadioByValue(oDocument As Object, sRadioValue$) As Boolean
With oDocument.All
On Error Resume Next
For i = 0 To (.Length - 1)
If .Item(i).Type = "radio" And .it[red]em[/red](i).Value = sRadioValue Then
On Error GoTo 0
[red]If .Item(i).Click = True Then[/red]
SelectRadioByValue = True
Exit Function
[red]End If[/red]
End If
Next
On Error GoTo 0
End With
SelectRadioByValue = False
End Function

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Here's the script I used:
Code:
Dim xItem
Dim oIE
Set oIE = CreateObject("InternetExplorer.application")
With oIE
    .Visible = True
    .navigate ("file://c:/scripttest.html")
End With
Do Until oIE.ReadyState = 4  'readystate 4 = done loading
    wscript.sleep 200
Loop
With oIE.document.All
    on error resume next
    'be sure your input boxes are named like: <input type=text name="login">
    .Item("login").Value = "Tek-Tips rocks" 'Set username
    .Item("passwd").Value = "fakepass" 'Set password
    .Item("offers").Click 'Put a check in a checkbox
    .Item("dropdown1").Value = "2" 'When setting drop down values, you have to use the value of the <otion> you want to select
    If SelectRadioByValue(oIE.document.All, "Mr") = True Then
        'Radio button was found and clicked
         MsgBox "Verify each object is filled out and hit OK to test submit"
	 .Item("submit").Click 'Click Submit
    Else
        MsgBox "bummer"
    End If
End With


Function SelectRadioByValue(oDocAll, sRadioValue)
    With oDocAll
        On Error Resume Next
        For i = 0 To (.Length - 1)
            sValue = Empty
            sType = Empty
            sValue = .Item(i).Value
            sType = .Item(i).Type

            If Not IsEmpty(sValue) And Not IsEmpty(sType) Then
                If .Item(i).Type = "radio" Then
                    On Error GoTo 0
                    If .Item(i).Value = sRadioValue Then
                        .Item(i).Click
                        SelectRadioByValue = True
                        Exit Function
                    End If
                End If
            End If
        Next
        Stop
        On Error GoTo 0
    End With
    SelectRadioByValue = False
End Function

Here's the html test page I used (call it scripttest.html and place in root of C if using to test:
Code:
<form method=get action=scripttest.html>
<table>
	<tr>
		<td>
<table border="0" cellpadding="2" cellspacing="0">
	<tr>
		<td align="right" nowrap  class="bodywhite">Login:</td>
		<td align="right"><input name="login" size="17" value=""></td>
	</tr>
	<tr>
		<td align="right" nowrap class="bodywhite">Password:</td>
		<td align="right"><input name="passwd" size="17" value="" type="password"></td>
	</tr>
	<tr>
		<td colspan="2" nowrap align="right" class=bodywhite><input name="offers" type="checkbox" id="offers" value="yes">Offers</td>
	</tr>
	<tr>
		<td colspan="2" align="right"><input type="RADIO" name="prefix" value="Mr" >Mr.<input type="RADIO" name="prefix" value="Mrs" >Mrs.<input type="RADIO" name="prefix" value="Miss" >Miss <input type="RADIO" name="prefix" value="Ms" ></TD>
	</tr>
	<tr>
		<td colspan="2" align="right"><select name="Dropdown1" value="">
					      <option value="1" Selected>a</option>
					      <option value="2">b</option>
					      <option value="3">c</option>
					      <option value="4">d</option>
					      <option value="5">e</option>
					      <option value="6">f</option></select>Drop Down<BR><BR></td>
	</tr>
	<tr>
		<td colspan="2" align="right"><input name="submit" type="submit" value="Sign In" class=buttonwhite></td>
	</tr>
</table>
</form>

I was having issues with the code continuing in the function I made if I checked for .item.value or .type so I just set variables to those values and it worked ok. If somebody wants to clean it up (and actually test that their changes work) then feal free.

Thanks for fixing my typo BTW Tom I'm sure it saved me allot of time.

By the way, in VBScript, is the starting sub supposed to be in a Sub... End Sub section? If so what's it called (ie Sub Main()?? Also there anything similar to the Declarations in VB that you can dim a variable so it is accessible by all subs/functions?

Hayesb2,
I found this helpful to me when I had to allot of automating for IE. Its a macro I quickly threw together for Excel:
Code:
Sub getDocItems()
    Cells.Delete
    Dim objIE
    Set objIE = CreateObject("internetexplorer.application")
    objIE.navigate ("mail.yahoo.com")
    Do Until objIE.readystate = 4
        DoEvents
    Loop
    With objIE.document.all
        On Error Resume Next
        For i = 0 To .Length - 1
            Range("b" & Range("a65536").End(xlUp).Row).Offset(1, 0) = "NodeName: " & .Item(i).nodeName
            Range("c" & Range("a65536").End(xlUp).Row).Offset(1, 0) = "Name:" & .Item(i).Name
            Range("d" & Range("a65536").End(xlUp).Row).Offset(1, 0) = "Value: " & .Item(i).Value
            Range("e" & Range("a65536").End(xlUp).Row).Offset(1, 0) = "Type: " & .Item(i).Type
            Range("f" & Range("a65536").End(xlUp).Row).Offset(1, 0) = "Text: " & .Item(i).Text
            Range("a" & Range("a65536").End(xlUp).Row).Offset(1, 0) = "item: " & i
        Next
        On Error GoTo 0
    End With
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top