【问题标题】:How to fill webform如何填写网络表格
【发布时间】:2016-09-11 22:07:46
【问题描述】:

我想使用 Excel 值更新网站中的字段。

我在网上找到了这个宏并尝试编辑它。

登录成功后如何更新字段?

我添加了以下参考:

  • Microsoft HTML 对象库
  • Microsoft Internet 控件

我尝试在 IE8 中运行它。

收到错误:

对象'IWebBrowser2'的方法'Document'在线失败

我更新到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">&#160;Caps Lock is on.</td></tr></table></span>
        </td>
    </tr>
</table>
<div class="div-submit">&#160;</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">

【问题讨论】:

  • 你试过.InnerText吗?
  • 真正的问题是什么?

标签: excel vba internet-explorer-11


【解决方案1】:

这个:

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 中尚不可用。


补充说明:

  • 当您使用它时,我可能也会在 API 声明中将 HWND 更改为 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
    
  • 具有 复数 名称的 DOM 方法(getElements

  • 您实际上并没有在任何时候关闭 IE,您只是从内存中释放对象。在设置为Nothing之前使用IE.Quit关闭应用程序

  • 最后,不需要endmacro: 标签,因为您不会在任何时候更改错误处理或指导代码。


在 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

【讨论】:

  • 非常感谢宏先生。由于网络表单填写对我来说是新事物,因此我遇到了问题。我将这些行更改如下: IE.document.getElementsByName("_58_login")(0).Value = "jigar@jigar.com" IE.document.getElementById("_58_password").Value = "****** *”现在我得到错误“对象变量或未设置块变量请帮助。
  • getElementsByName 仍然不正确 - 应该是 getElementsByClassName。这取决于您是否实际上针对您使用的方法的 class/Id/tag 属性。在您的问题中发布 html 会更有帮助
  • 现在 IE.Document.getElementById("_58_password").Value = "Password" 行出现“需要对象”错误
  • 它在一个表格中,这有点棘手 - 你必须尝试获取不同的 HTMLCollections 并遍历它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
相关资源
最近更新 更多