GTB
GTB

Reputation: 5

VBA: Find and update Input field in HTML form

I am trying to access and enter a text string in the address field in an HTML form, then click the submit form button. I tried using:

Set ObjCollection = ObjIE.Document.getElementsByTagName("input")

but this doesn't work because it only finds input tags and my input field seem to be part of a list.

Here is the HTML source which seems to be in Javascript:

 <script type="text/javascript"

I'm trying to access the input id="addressID" and submit using the input type="button" id="SubmitForm" (lines 5 and 6):

<div id="searchbar">
  <form id="addressForm" action="#" onsubmit="" >
    <fieldset><legend>Locate address</legend>
        <ul class="formFields">
          <li><label for="address">Address</label><input id="addressID" size="60" class="text"      type="text" name="address" /></li>
          <li><input type="button" id="SubmitForm" value="Find address" class="form-button" /> </li>
       </ul>
   </fieldset>    
  </form>
  <form id="SolarViewForm" action="#"  id="sideseach">
   <fieldset><legend>Create SolarView</legend>
   <ul class="formFields">
   <li>
  <label for="latitude">Latitude</label>
  <input id="id_latitude" size="10" maxlength="30" type="text" name="latitude" />
</li>
<li>
  <label for="longitude">Longitude</label>
  <input id="id_longitude" size="10" maxlength="30" type="text" name="longitude" />
</li>
<li>
  <label for="zenith_angle">Panel tilt (degrees)-optional</label>
  <input id="id_zenith_angle" size="10" maxlength="30" type="text" name="zenith_angle" />
</li>
<li>
  <label for="azimuth_angle">Panel bearing (degrees)-optional</label>
  <input id="id_azimuth_angle" size="10" maxlength="30" type="text" name="azimuth_angle" />
</li>
<li>
  <label for="image_title">Image title</label>
  <input id="id_image_title" size="10" maxlength="30" type="text" name="image_title" />
</li>
<input type="hidden" name="_csrf_token" value="5e3de3851eafb868ee9f64e5ea1136e2" id="csrf_token"   />   <li><input type="button" id="create" class="form-button"
value="Create SolarView" /></li>

  </ul>
  </fieldset>
  </form>

   </div>

I want to use this sort of method to update the field and click on the button:

SiteAddress = ActiveSheet.Range("B3").Value & ", " & ActiveSheet.Range("B4").Value & ", " & ActiveSheet.Range("B5").Value

Set ObjCollection = ObjIE.Document.getElementsByTagName("input")

i = 0
While i < ObjCollection.Length
    If ObjCollection(i).Name = "address" Then

        ObjCollection(i).Value = SiteAddress

    Else
            If ObjCollection(i).Type = "button" And _
            ObjCollection(i).ID = "SubmitForm" Then

        Set objElement = ObjCollection(i)

        End If
    End If
    i = i + 1
Wend
    objElement.Click

I have been trying to gather a collection of tags to loop through the fields using this code:

Set ObjCollection = ObjIE.Document.All.searchbar.getElementsByTagName("input")

I got this idea from a very good post here:

VBS: target fields by div and tabIndex from external .vbs

and I sense that it's closer to solving my problem, although this still only comes up with the same result as:

Set ObjCollection = ObjIE.Document.getElementsByTagName("input")

A big part of the problem for me seems to be not asking the right question, so I've spent ages trying to find an answer to this problem and I would be hugely grateful for a suitable answer.

Upvotes: 0

Views: 3268

Answers (1)

Hubvill
Hubvill

Reputation: 504

If you want to enter text into the address text box use this:

IE.document.getElementById("addressID").Value = "test"

If you want to click the button use this:

IE.document.getElementById("SubmitForm").Click

At least that worked for me. Let me know how it goes.

Upvotes: 1

Related Questions