如何使用vb6.0输入登录窗口的用户名和密码?

时间:2012-02-23 11:53:11

标签: vb6

我正在开发一个项目,我想以编程方式插入登录信息。下面是我的代码和我要添加登录信息的窗口图像。

'Reference: Microsoft HTML Library
'Control: Webbrowser Control

Dim HTML As HTMLDocument
Dim HTMLI As HTMLInputElement
Dim uName, uPass As String


Private Sub form_load()

WebBrowser1.resizable = True
WebBrowser1.navigate "http://<any website name>"

Call autologin
End Sub

Sub autologin()

On Error Resume Next

uName = "user"
uPass = "password"


Set HTML = WebBrowser1.document

    For Each HTMLI In HTML.getElementsByTagName("input")


        If HTMLI.Name = "Login" Then

        HTMLI.Value = uName

        End If


        If HTMLI.Type = "password" Then
        HTMLI.Value = uPass
        End If
        If HTMLI.Type = "ok" Then
        HTMLI.Click
        End If

    Next
End Sub 

enter image description here

在上面的代码中,我陷入了这个登录窗口

1 个答案:

答案 0 :(得分:2)

这有一些问题。首先,您没有给网页加载机会。在Web浏览器的DocumentComplete事件中执行AutoLogin()。此外,如果ok按钮不在INPUT标签集合的末尾,那么您可以提交没有所有数据的表单。

以下是我向您展示的代码:

Option Explicit

Private Const m_sURI           As String = "http://<any website name>"

Private Sub Form_Load()

    WebBrowser1.Resizable = True
    WebBrowser1.Navigate m_sURI

End Sub

Sub autologin()

    Dim HTML As HTMLDocument
    Dim HTMLI As HTMLInputElement
    Dim inputButton As HTMLInputElement
    Dim uName As String
    Dim uPass As String

    On Error GoTo ErrorHandler:

    uName = "user"
    uPass = "password"

    Set HTML = WebBrowser1.Document

    For Each HTMLI In HTML.getElementsByTagName("input")
        If HTMLI.Name = "Login" Then
            HTMLI.Value = uName
        End If
        Select Case HTMLI.Type
        Case "password"
            HTMLI.Value = uPass
        Case "ok"
            ' Save the ok reference so we can use it later.
            Set inputButton = HTMLI
        End Select
    Next HTMLI

    If inputButton Is Nothing Then
        MsgBox "Ok button not found."
    Else
        inputButton.Click
    End If
Exit Sub

ErrorHandler:
    ' Handle error ... 
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)

    If URL = m_sURI Then
        autologin
    End If

End Sub