VBA和Internet Explorer:填写输入框
我不熟悉VBA和Internet Explorer之间的交互,但是我在网上读过很多东西,在我的代码中找不到问题所在。我只想检索网站上的“用户名”框,并在其中添加一个值。因此,我将所有输入框检索到一个HTML元素集合中,但该集合为空:VBA和Internet Explorer:填写输入框,vba,internet-explorer,Vba,Internet Explorer,我不熟悉VBA和Internet Explorer之间的交互,但是我在网上读过很多东西,在我的代码中找不到问题所在。我只想检索网站上的“用户名”框,并在其中添加一个值。因此,我将所有输入框检索到一个HTML元素集合中,但该集合为空: Dim Collect As IHTMLElementCollection With IE .navigate "http:xxxxxxxxxx" .Visible = True End With Do While IE.Busy Loop S
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
现在一切都很好。感谢您的回复。请根据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
你能发布输入框的HTML吗?