【问题标题】:VBA and Internet Explorer: fill in an input boxVBA 和 Internet Explorer:填写输入框
【发布时间】:2015-02-03 17:29:10
【问题描述】:

我是在 VBA 和 Internet Explorer 之间进行交互的新手,但我在网上阅读了很多内容,但无法找出我的代码中的问题。我只想检索网站上的“用户名”框并在其中添加一个值。所以我将所有输入框检索到一个 HTML 元素集合中,但是那个集合是空的:

Dim Collect As IHTMLElementCollection

With IE
    .navigate "http:xxxxxxxxxx"
    .Visible = True
End With

Do While IE.Busy
Loop

Set Collect = IE.document.getElementsByTagName("input")
MsgBox Collect.Length
End Sub

这将给出一个带有“0”的消息框。如果我在代码结束前切换断点并“观察”变量Collect,我可以看到里面有 17 个项目,其中之一是用户名“inputbox”,名称为“tfUserName”。你能帮帮我吗?


编辑:我发现问题来自这段代码:

    Do While IE.Busy
    Loop

我用这个代替了:

    Do Until IE.readyState = READYSTATE_COMPLETE
        DoEvents
    Loop

现在一切正常。感谢您的回复。

【问题讨论】:

  • 可以把输入框的HTML贴出来吗?

标签: vba internet-explorer


【解决方案1】:

根据 null 验证集合以确定它是否包含元素

If Not Collect Is Nothing Then
  For Each htmlElement In Collect
    If Not htmlElement.getAttribute("username") Is Nothing Then
       htmlElement.setAttribute("value", "licou6")
       Exit For
    End If
  Next
End If

【讨论】:

    猜你喜欢
    • 2015-02-13
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多