IE Automation(IE8)使用VBA和输入类型“隐藏”

时间:2015-11-13 21:35:49

标签: vba internet-explorer

我正在使用vba在Internet Explorer中自动化网站。到目前为止我一直非常成功。我遇到的问题是有一个输入字段,类型是“隐藏”而不是文本。我该如何解决这个问题?我知道必须有办法。

感谢您的任何建议。

2 个答案:

答案 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

代码为我提供了以下输出:

immediate window

如您所见,它隐藏了主输入标记,输出了隐藏输入标记#4的值,并更改了#4和#5的类型和值,因此网页看起来像:

modified web page

答案 1 :(得分:1)

CSS选择器:

您可以使用CSS选择器来定位input[type='hidden']的那些元素。读为带有input标签,属性为type,值为'hidden'的元素。


CSS查询(示例结果):

CSS query


之前的样本(样本):

Before


(样本)之后的页面:

After


VBA:

您可以使用querySelectorAll的{​​{1}}方法应用css选择器,然后循环返回的nodeList的长度。

我通过用document交换"hidden"的值使之可见,并使用vbNullString属性分配一个值。

value