VBA雅虎login
我试图让VBA上的macroslogin到雅虎。 但是由于用户名/密码在不同的页面上,我也无法提交密码部分。
我不断收到错误
运行时错误“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元素不beeingfind,但我已经尝试添加一个延迟,另一个代码的等待页面加载,没有seens工作。
以下是这两个字段的特定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
error handling是通过标签完成的,并且可以非常快速地变得非常难看。
您需要定义一个标签和一个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