我正在使用vba自动化Internet Explorer中的站点。在这一点上,我一直非常成功。我遇到的问题是有一个输入字段,类型是“隐藏”,而不是文本。我如何解决这个问题?我知道必须有一种方式。使用VBA和输入类型“隐藏”的IE自动化(IE8)
感谢您的任何建议。
我正在使用vba自动化Internet Explorer中的站点。在这一点上,我一直非常成功。我遇到的问题是有一个输入字段,类型是“隐藏”,而不是文本。我如何解决这个问题?我知道必须有一种方式。使用VBA和输入类型“隐藏”的IE自动化(IE8)
感谢您的任何建议。
下面的例子演示了如何通过指数环是指输入元素:
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的类型和值,因此网页看起来像:
比你非常感谢你的帮助。我仍然对如何将文本元素放入类型为“隐藏”的输入框感到困惑,我似乎无法获得改变的价值。 – hockeybo97
@ hockeybo97,我添加了描述如何更改隐藏的输入内容。 – omegastripes
非常感谢。我已经获得了成功改变的价值。我现在面临的挑战是如何将它放到窗体上的输入框中。这里是一个源代码的例子我想让Input Name等于我指定的值。 – hockeybo97
我猜你正在处理网页表单,那么你可以分享表单的HTML内容吗?还有你的VBA代码? – omegastripes
如果您显示您尝试的代码并更准确地解释您收到了哪些错误或哪些工作不正常,那更好。通常,您将参考设置为隐藏表单字段,您可以像设置常规文本输入字段一样设置其值。 –