hockeybo97
hockeybo97

Reputation: 21

IE Automation (IE8) using VBA and Input type "hidden"

I am using vba to automate a site in Internet Explorer. I have been very successful to this point. The issue I am running into is that there is an input field and the type is "hidden" and not text. How do I get around this? I know there is got to be a way.

Thanks for any suggestions.

Upvotes: 1

Views: 2522

Answers (2)

QHarr
QHarr

Reputation: 84465

CSS selector:

You can use a CSS selector to target those elements of input[type='hidden']. This reads as elements with input tag, having attribute type, with value 'hidden'.


CSS query (sample results):

CSS query


Sheet before (sample):

Before


Sheet after (sample):

After


VBA:

You apply the css selectors with the querySelectorAll method of document and then loop the length of the returned nodeList.

I make visible by swopping the "hidden" value with vbNullString and assign a value using the value attribute.

Option Explicit
Public Sub AlterHidden()
    Dim ie As New InternetExplorer, aNodeList As Object, i As Long
    Const URL As String = "https://uk.yahoo.com"
    With ie
        .Visible = True
        .navigate URL
        While .Busy Or .readyState < 4: DoEvents: Wend
        On Error GoTo noCookieRequest
        .document.querySelector("input[value='OK']").Click '<==On time required for cookies
        While .Busy Or .readyState < 4: DoEvents: Wend
noCookieRequest:
       Do
        On Error Resume Next
        Set aNodeList = .document.querySelectorAll("input[type='hidden']")
        On Error GoTo 0
        Loop While aNodeList Is Nothing
        For i = 0 To aNodeList.Length - 1
            With aNodeList.item(i)
                .Type = vbNullString
                .Value = "Surprise " & i
            End With
        Next i
        Stop                                     '<== Delete me
        .Quit
    End With
End Sub

Upvotes: 1

omegastripes
omegastripes

Reputation: 12602

The below example demonstrates how to refer to input elements by index in loop:

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

The code gives for me the following output:

immediate window

As you can see, it hides the main input tag, outputs the value of the hidden input tag #4, and change the type and the values of #4 and #5, so the web page looks like:

modified web page

Upvotes: 2

Related Questions