尝试从 Excel VBA 登录网页,但用户名和密码显示为灰色

约翰福勒

我正在尝试使用 Excel 中的 VBA 代码自动登录网页。我可以设置用户名值和密码值,但它显示为灰色并且网页似乎无法识别它。当我将点击事件发送到登录按钮时,它说我没有输入用户名和密码。我尝试先将焦点设置为用户名和密码字段,但没有成功。

Sub test_login()
    Set obj_Shell = CreateObject("Shell.Application")
    IE_count = obj_Shell.Windows.count
    On Error Resume Next
    For x = 0 To (IE_count - 1)
        my_url = obj_Shell.Windows(x).Document.Location
        ' actual url=http://community.betfair.com
        If my_url Like "*community*" Then
            Set ie = obj_Shell.Windows(x)
            Exit For
        Else
        End If
    Next
    On Error GoTo 0
    Set username_box = ie.Document.getelementbyid("username")
    username_box.Value = "myusername"
    Set password_box = ie.Document.getelementbyid("passwordText")
    password_box.Value = "mypassword"
    Set login_button = ie.Document.getelementbyid("login")
    login_button.Click

End Sub
哈尔

该页面需要通过按键输入文本,这意味着我认为需要可怕的 SendKeys。以下工作:

Option Explicit
Public Sub GetInfo()
    Dim IE As New InternetExplorer
    With IE
        .Visible = True
        .Navigate2 "http://community.betfair.com/"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document

            With .getElementById("username")
                .Focus
                SendKeys "Bob"
            End With

            SendKeys "{tab}"

            Application.Wait Now + TimeSerial(0, 0, 1)

            With .getElementById("passwordText")
                .Focus
                SendKeys "Banana"
            End With

            Application.Wait Now + TimeSerial(0, 0, 1)

            .getElementById("login").Click

            While IE.Busy Or IE.readyState < 4: DoEvents: Wend

            Stop '<==Delete me later

        End With
        .Quit
    End With
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章