VBA雅虎登录



我正在尝试在VBA上制作一个宏以登录到雅虎。但是我也在提交密码部分时遇到了麻烦,因为用户名/密码位于不同的页面上。

我不断收到错误

需要运行时错误"424"对象

.Document.getElementById("login-passwd").Value线上。

    Sub WebLogin()
Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Visible = True
    .Navigate "https://login.yahoo.com/"
    Do Until .ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    .Document.getElementById("login-username").Value = "username"
    .Document.getElementById("login-signin").Click
    Do Until .ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    'Application.Wait (Now + TimeValue("0:00:10"))
    .Document.getElementById("login-passwd").Value = "password*"
    .Document.getElementById("login-signin").Click
End With
End Sub

我想找不到 HTML 元素,但我已经尝试添加延迟,另一个代码用于等待页面加载并且看不到任何工作。

以下是这两个字段的特定 HTML 代码。

<input name="username" tabindex="1" class="phone-no " id="login-username" autofocus="true" type="text" placeholder="Insira seu e-mail" value="" autocorrect="off" autocapitalize="none">
<input type="password" id="login-passwd" name="password" placeholder="Senha" autofocus="">

有什么建议吗?

引发运行时错误"424"IE.Document.getElementById("login-passwd"),因为它不存在,并且您尝试访问其.Value

VBA中的错误处理是通过标签完成的,并且很快就会变得非常丑陋。

您需要定义标签和On Error语句

让我们定义这个子例程:

Sub WaitAndFeed(ByRef IE, ByVal field As String, ByVal value As String)
    On Error GoTo NoObjErr
    While Not (IE.Document.getElementById(field) Is Nothing)
    NoObjErr:
        DoEvents
        IE.Document.getElementById(field).Value = value
        IE.Document.getElementById("login-signin").Click
    Wend
End Sub

您的代码现在应修改为:

Sub WebLogin()
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .Navigate "https://login.yahoo.com/"
        Do Until .ReadyState = READYSTATE_COMPLETE
            DoEvents
        Loop
    End With
    Call WaitAndFeed(IE, "login-username", "username")
    Call WaitAndFeed(IE, "login-passwd", "password")
End Sub

最新更新