How to submit a web form without a name?

musicgold

Board Regular
Joined
Jan 9, 2008
Messages
197
Hi,

I am trying to submit a web form using VBA. The problem is there is no name to this form. There is a login button, but again it has no name, so I am not able to access it.
I am able to access the inputs : username and password using getElementsByName.

HTML source code of the page.

HTML:
<form action="pilatthn.cgi" method="post" target=_top>
     <table width="450" border="0" cellspacing="0" cellpadding="2" summary="Login">
        <tr>
          <td width="129"> Username:</td>
          <td><input name="user" type="text"><font size=2>   </font></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input name="pass" type="password"><font size=2>  </font></td>
        </tr>
        <tr>
          <td colspan="2"><p>
              <input type="submit" value="Login">
            </td>
        </tr>
      </table>
	<input type="hidden" name="url" value="http://www.google.com">
	</form>


Thanks,

MG.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi...


Check below code...
You can just check the type of control and access that...

Code:
Sub test()
    
    Dim ie As InternetExplorer
    
    Set ie = New InternetExplorer
    
    ie.Navigate [COLOR="Red"]"D:\VBA Forum\Testing\test.html"[/COLOR]
    
    ie.Visible = True
    
    'Loop unitl ie page is fully loaded
    Do Until ie.ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    Do Until ie.Document.ReadyState = "complete"
        DoEvents
    Loop
    
    ie.Document.all.Item("user").Value = "Username"
    ie.Document.all.Item("pass").Value = "Password"
    
    Dim x As Integer
    Dim i As Integer
    
    For i = 0 To ie.Document.forms(0).Length - 1
        
        If ie.Document.forms(0)(i).Type = "submit" Then
            ie.Document.forms(0)(i).Click
            Exit For
        End If

    Next i
    
    Do Until ie.ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    Do Until ie.Document.ReadyState = "complete"
        DoEvents
    Loop
    
    
    ' Your code
    
    
End Sub

Note - Update your URL in code...
 
Upvote 0
somnath_it2006,

Thanks. It works great! However, I don't understand two points.

1. How does one know that the object ( the button) is on form (0)?

2. Why there is no dot between forms(0) and (i) ? For example, in "ie.document.all.Item("pass")" there is a dot between an object and its child object.


Thanks again,

MG.
 
Upvote 0
We can get the total count of forms on webpage using "ie.Document.forms.Length - 1":

Code:
    For x = 0 To ie.Document.forms.Length - 1
 
        For i = 0 To ie.Document.forms(x).Length - 1
 
            'MsgBox ie.Document.forms(x)(i).Value & " " & ie.Document.forms(x)(i).Type
 
            If ie.Document.forms(x)(i).Value = "Set" And ie.Document.forms(x)(i).Type = "submit" Then
                ie.Document.forms(x)(i).Click
                Exit For
            End If
 
        Next i
 
    Next x

So the x is form counter and i is controls counter..


And the dot property, I don't know why it is not there...
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,974
Members
448,934
Latest member
audette89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top