我正在使用vba在Internet Explorer中自动化网站。到目前为止我一直非常成功。我遇到的问题是有一个输入字段,类型是“隐藏”而不是文本。我该如何解决这个问题?我知道必须有办法。
感谢您的任何建议。
答案 0 :(得分:2)
下面的示例演示了如何通过循环索引引用输入元素:
Sub Test()
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
' navigate and download the web page
objIE.Navigate "https://www.yahoo.com/"
Do While objIE.ReadyState <> 4 Or objIE.Busy
DoEvents
Loop
' retrieve document object
Set objDocument = objIE.document
' retrieve forms collection
Set colForms = objDocument.forms
' retrieve the first form object by index
Set objForm = colForms(0)
' retrieve the form input tags collection
Set colInputTags = objForm.getElementsByTagName("input")
' loop through all input tags in the form
For n = 0 To colInputTags.Length - 1
' refer to the certain input tag by index
Set objInputTag = colInputTags(n)
' output
Debug.Print n _
& " (" & objInputTag.Type & ") " _
& objInputTag.Name & " = " _
& objInputTag.Value
Next
' refer to input tag #0
Set objElement = colInputTags(0)
' hide the search inputbox
objElement.Type = "hidden"
' refer to input tag #4
Set objElement = colInputTags(4)
' output the current value
Debug.Print "#4 value = " & objElement.Value
' cnange the value
objElement.Value = "input tag #4"
' cnange the type
objElement.Type = "Text"
' refer to input tag #5
Set objElement = colInputTags(5)
' cnange the value
objElement.Value = "input tag #5"
' cnange the type
objElement.Type = "Text"
' quit IE
objIE.Quit
End Sub
代码为我提供了以下输出:
如您所见,它隐藏了主输入标记,输出了隐藏输入标记#4的值,并更改了#4和#5的类型和值,因此网页看起来像:
答案 1 :(得分:1)