VB填充IE上的输入框并提交
假设登录页面的HTML代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>testPage</title>
</head>
<body>
<FORM name=chh method = "POST" action = "login.asp">
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td width="31%" align="center">用户:</td>
<td width="69%" align="left">
<input name="LoginName" type="text" id="LoginName" size="12">
</td>
</tr>
<tr>
<td width="31%" align="center">密码:</td>
<td width="69%" align="left">
<input name="LoginPassword" type="password" id="LoginPassword" size="12">
</td>
</tr>
</table>
<input type = "submit" id = "clickme" value = "提交">
</FORM>
</body>
</html>
很简单的页面,在上面有一个登录的Form,一个用户名输入框LoginName,一个密码输入框LoginPassword和一个提交按钮clickme
然后用IE打开这个页面,注意页面的title设置为testPage。
然后打开VB,在工程中引用Microsoft internet controls:
Private Sub Command1_Click()
Dim IEList As New ShellWindows
Dim browser
Dim Doc
On Error Resume Next
'遍历当前地浏览器窗口
For Each browser In IEList
'找到需要地IE窗口
If browser.Document.Title = "testPage" Then
'获得浏览器地文档对象
Set Doc = browser.Document
'填写用户名字段
Doc.body.All("LoginName").Value = "eddie"
'填写密码字段
Doc.body.All("LoginPassword").Value = "123456"
'提交
Doc.body.All("clickme").Click
End If
Next
End Sub
怎么编程把用户名,密码提交到网页上的登录页?
首先在程序中加入Webbrowser控件并加入引用 Microsoft HTML Object Library。
假设你的HTML页面表单代码如下:
注意其中元素的type、Name、value属性。然后VB中的代码如下:
Private Sub Command1_Click()
WebBrowser1.Navigate "http://chen/chat/newuser.htm"
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim vDoc, vTag
Dim i As Integer
Set vDoc = WebBrowser1.Document
List1.Clear
For i = 0 To vDoc.All.length - 1
If UCase(vDoc.All(i).tagName) = "INPUT" Then
Set vTag = vDoc.All(i)
If vTag.Type = "text" Or vTag.Type = "password" Then
List1.AddItem vTag.Name
Select Case vTag.Name
Case "Name"
vTag.Value = "IMGod"
Case "NickName"
vTag.Value = "IMGod"
Case "Password"
vTag.Value = "IMGodpass"
Case "EMail"
vTag.Value = "IMGod@paradise.com"
End Select
ElseIf vTag.Type = "submit" Then
vTag.Click
End If
End If
Next i
End Sub
点击Command1就可以自动填表并提交了。