代碼登陸網站
以下介紹一段登陸網站的代碼,此代碼嘗試找出 登陸 HTML Form 內的 UserName 及 Password 位置然後自動填入。
'
' Set reference to Microsoft HTML Object Library
' Set Reference to Microsoft Internet Controls
'
Option Explicit
Dim myIE As InternetExplorer
Dim myIEdoc As HTMLDocument
Dim theForm As HTMLFormElement
Sub OpenWebpageAndLogin(URL As String, myPW As String, myID As String)
Dim theItm As HTMLFormElement
Dim i As Integer
Dim flg As Boolean
Set myIE = New InternetExplorer
With myIE
.Navigate URL
.Visible = True
Do While .Busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
End With
Set myIEdoc = myIE.Document
Set theForm = findFm(myIEdoc)
With theForm
For i = 0 To .Length - 1
Select Case .Item(i).Type
Case "password"
.Item(i).Value = myPW
flg = True
Case "text"
.Item(i).Value = myID
Case Else
End Select
Next
End With
If flg Then
theForm.submit
Else
myIE.Quit
MsgBox ("Unexpected Error, I'm quitting.")
End If
Set myIE = Nothing
End Sub
Function findFm(theDoc As HTMLDocument) As HTMLFormElement
Dim i As Integer, j As Integer
Dim theItm As HTMLFormElement
With theDoc.forms
For i = 0 To .Length - 1
Set theItm = .Item(i)
With theItm
For j = 0 To .Length - 1
If .Item(j).Type = "password" Then
Set findFm = theItm
Exit Function
End If
Next
End With
Next
End With
End Function
Sub Test()
OpenWebpageAndLogin "http://mysinablog.com/admin.php", "Password", "UserName"
End Sub




