2014-12-05 112 views
0

我是新来的VBA和Internet Explorer之间的交互,但我已经在网上阅读了很多东西,无法找出我的代码中的问题。我只想检索网站上的“用户名”框并在其中添加值。所以,我检索到的所有输入框到HTML元素的集合,但那么集合为空:VBA和Internet Explorer:填写输入框

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 

而现在一切工作正常。谢谢你的回复。

+0

您可以张贴在输入框的HTML? – 2014-12-05 15:32:14

回答

0

验证对空的集合,而不是以确定它是否包含元素

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