VBA填补webform

我正在做一些自动化,其中数据将在Excel中,并需要使用Excel值更新网站中的字段。 我发现这个macros在线,并试图编辑它,但它不工作。 请build议,所以我可以进一步进行字段更新一旦login成功。

我已经添加了以下的信息:

  • Microsoft HTML对象库
  • 微软互联网控制

问题:试图在IE8中运行它。 我需要更新到IE 11吗? 收到错误:

对象“IWebBrowser2”的方法“文档”在线失败

更改:我已经更新到IE11,现在错误更改为

“自动化错误,未指定的错误”
IE.document.getelementsbyname("_58_login").Value = "jigarjigar"

 Public Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long Sub Automate_IE_Enter_Data() 'This will load a webpage in IE Dim i As Long Dim URL As String Dim IE As Object Dim objElement As Object Dim objCollection As Object Dim HWNDSrc As Long Dim document As HTMLDocument 'Create InternetExplorer Object Set IE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}") 'Set IE = CreateObject("InternetExplorer.Application") 'True to make IE visible, or False for IE to run in the background IE.Visible = True 'Navigate to URL IE.Navigate https://www.asite.com/login-home/ ' Wait while IE loading... Do While IE.ReadyState = 4: DoEvents: Loop 'Get Window ID for IE so we can set it as activate window HWNDSrc = IE.HWND 'Set IE as Active Window SetForegroundWindow HWNDSrc 'Find & Fill Out Input Box IE.document.getelementsbyname("_58_login").Value = "jigarjigar" IE.document.getelementsbyname("_58_password").Value = "mypassword" IE.document.getelementsbyclassname("btn-submit nobgcolor").Click 'Unload IE endmacro: Set IE = Nothing Set objElement = Nothing Set objCollection = Nothing End Sub 
  <table class="lfr-table"> <tr> <td class="label-unm"> Login (Email) </td> <td> <div class="inp-login"><input name="_58_login" type="text" value="jigar@jigar.com" onblur="checkforGSUser(this)" autocomplete="off"/></div> </td> </tr> <tr> <td class="label-pass"> Password </td> <td> <div class="inp-login"><input id="_58_password" name="_58_password" type="password" autocomplete="off" value="" /></div> <span id="_58_passwordCapsLockSpan" class="pwdCapsMsgSpan" style="position:absolute;display:none;"><table width="111" border="0" cellspacing="0" cellpadding="0"><tr><td class="pwdCapsBorder"><img src="/html/themes/asite/images/common/caps_msg_arrow.gif" hspace="10" /></td></tr><tr><td class="pwdCapsMsg"> Caps Lock is on.</td></tr></table></span> </td> </tr> </table> <div class="div-submit"> </div> <div class="div-login-link"> <input class="btn-submit nobgcolor" type="image" src="/html/themes/asite/images/common/login.gif" /> <a target="_self" href="https://portal.asite.com/widget/web/guest/home?p_p_id=58&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&_58_struts_action=%2Flogin%2Fview&_58_cmd=forgot-password">Forgot Password?</a> <br/> <a href="https://www.asite.com/contactus" target="_top">Don't have an account?</a> <br/> <div class="clear-all"></div> <div class="clear-all"></div> </div> </form> <script type="text/javascript"> 

这个:

 Do While IE.ReadyState = 4: DoEvents: Loop 

应该是这样的:

 Do While IE.ReadyState <> 4 Or IE.Busy: DoEvents: Loop 

IE.ReadyState枚举中,4 = READYSTATE_COMPLETE所以你告诉代码在页面完成服务时循环。

在此基础上,代码将不会等待页面加载(ReadyState 1至3),并且当您编辑DOM时,该元素在DOM中尚不可用。


进一步说明:

  • 当你在这个时候,我可能会把这个HWND LongPtr API声明中的一个LongPtr 。 (假设你正在使用x64版本 – 如果这将在其他机器上使用,你将需要看看使用条件编译)。

  • 此外, SetForegroundWindow方法返回一个Long ,在继续之前应检查该消息是否已成功发送。

  • 你的DOM方法看起来不对,你应该使用:

     IE.Document.getElementById("_58_login").Value '// Class names don't have spaces in them, so the below is also wrong IE.Document.getElementsByClassName("btn-submit nobgcolor")(0).Click '// Or IE.Document.Forms(0).Submit 
  • 具有复数名称( getElements <〜注意到's')的DOM方法返回一个HTMLColletion,需要迭代和testing,或者直接使用包含该项目索引的元素直接访问元素(如上面的例子 – 注意索引是从零开始的)

  • 在任何时候你都不会真的closuresIE,只需从内存中释放对象即可。 使用IE.Quitclosures应用程序,然后将其设置为Nothing

  • 最后,没有必要使用endmacro: label,因为在任何时候您都不会更改error handling或指导代码。


在OP中看到HTML之后:

 For Each el In IE.Document.GetElementsByTagName("input") If el.Name = "_58_login" Then el.Value = "jigar@jigar.com" Exit For End If Next IE.Document.GetElementById("_58_password").Value = "Password" IE.Document.GetElementsByClassName("btn-submit nobgcolor")(0).Click '// or IE.Document.Forms(0).Submit