Suren Grigoryan
Suren Grigoryan

Reputation: 91

Changing text box value on website using VBA

The HTML code is as follows: Password node:

<input name="customerPassword" type="password" class="customer-password form-control ng-dirty ng-valid-required filled ng-valid ng-valid-minlength" ng-class="{
  'input-error': submitted &amp;&amp; securityForm.customerPassword.$invalid,
  'filled': securityForm.customerPassword.$viewValue.length>0}" ng-minlength="7" maxlength="20" ng-model="customer.password" ng-keydown="checkEnterKey(event)" required="" autocomplete="new-password" aria-autocomplete="list">

Secuirty nodes

<li class="sec-number ng-scope control-disabled" ng-repeat="n in securityCode track by $index" ng-class="{'control-disabled': securityForm.customerPassword.$invalid}">
    <!-- ngIf: n -->
    <input name="secNumber1" type="password" maxlength="1" class="input-code form-control ng-scope ng-pristine ng-valid-pattern ng-invalid ng-invalid-required" ng-class="{'filled': customer.code[$index] !== undefined}" ng-model="customer.code[$index]" ng-if="n" ng-disabled="securityForm.customerPassword.$invalid" ng-pattern="/\d{1}$/" met-only-number="" required="" focus-next="" disabled="disabled">
    <!-- end ngIf: n -->

    <!-- ngIf: n --><b class="into-input position ng-binding ng-scope" ng-if="n">
                    1
                    <span class="ordinal ng-binding">st</span>
                </b>
    <!-- end ngIf: n -->

    <!-- ngIf: !n -->
</li>

"secNumber" can be anything between 1 to 8. Part of VBA code is as follows:

Set oPassword = .document.getElementsByName("customerPassword")(0)
oPassword.Focus
oPassword.Value = Password
oPassword.Focus

Dim strsecnum As String

For i = 1 To 8

    strsecnum = "secNumber" & i

    For Each ele In .document.getElementsByName(strsecnum)

        Set ele = .document.getElementsByTagName(strsecnum)(0)
        ele.Value = i

    Next

Next i

I am getting error 91, "Object Variable or With Block Variable not set" on ele.value = i. ele is defined as Object, so I am not sure what is the problem.

Upvotes: 1

Views: 804

Answers (1)

QHarr
QHarr

Reputation: 84475

Your error is likely occurring because you are assuming all 8 possibilities exist in the code above.

You can alter the loop and use a css attribute = value selector with starts with operator to target only values which exist. As the values are between 1-8, and we can see the expected values are indeed single digit from html (ng-pattern="/\d{1}$/"), we can assign the value to the right based on the left hand side name attribute's value:

Dim i As Long

For i = 0 To ie.document.querySelectorAll("[name^='secNumber']").Length - 1
    ie.document.querySelectorAll("[name^='secNumber']").item(i).value  = Right$(ie.document.querySelectorAll("[name^='secNumber']").item(i).getAttribute("name") , 1)
Next 

If you work with variables instead you will gain a small efficiency:

Dim i As Long, secNumberNodeList As Object, secNumberNode as Object

Set secNumberNodeList = ie.document.querySelectorAll("[name^='secNumber']")

For i = 0 To secNumberNodeList.Length - 1
    Set secNumberNode = secNumberNodeList.item(i)
    secNumberNode.value = Right$(secNumberNode.name , 1)
Next 

Note that attribute values are case sensitive, hence you need the s in secNumber. You can ignore case (for characters within the ASCII range) with i modifier.

Upvotes: 1

Related Questions