a.t. a.t. - 26 days ago 11
HTML Question

How to validate action on website by vba?

In an attempt to mark a checkbox in a website, I let vba open the site in ie. However even though the checkbox shows as being marked, I noticed the difference between when I let VBA do it, and manually clicked myself is that the

class
of the element changes to "dirty.." instead of "pristine.." after clicking.

Without that change to "dirty.." and touched, the website does not register the action as actually performed.

So I tried to set the properties of the "dirty..." class to checked, but it did not register the action as performed, So I want to compare all the properties of the "dirty.." and "pristine.." class, to see if I can make it register the action as performed by changing all the expressions required to make it dirty.

Hence, I made a manual comparison of the elemnt code before and after clicking by a manual user, and it is located in the vba code below as the first comments.

'Difference in html element code between vba clicking the checkbox and an actual user clicking the checkbox:
'<form name="forms.addressContract" class="form __hor ng-pristine ng-scope ng-invalid ng-invalid-required ng-valid-pattern ng-valid-maxlength" novalidate="" ng-class="{'force-show-errors': isSubmitted}" ng-submit="submitAddress()">
'<form name="forms.addressContract" class="form __hor ng-scope ng-invalid ng-invalid-required ng-valid-pattern ng-valid-maxlength ng-dirty ng-valid-parse" novalidate="" ng-class="{'force-show-errors': isSubmitted}" ng-submit="submitAddress()">
'<input class="ng-pristine ng-valid ng-touched" id="permission" type="checkbox" ng-model="permission.checked">
'<input class="ng-valid ng-touched ng-dirty ng-valid-parse" id="permission" type="checkbox" ng-model="permission.checked">

Set elements1a = ie.document.getElementsByClassName("ng-valid ng-touched ng-dirty ng-valid-parse")
MsgBox (elementa1a)
For Each Item In elements1a
If elements1a.Item.Checked = False Then
elements1a.Item.Click
If elements1a.Item.Checked = True Then
MsgBox ("clicking the checkbox switched the checked-ness of the item to true")
End If
End If
If elements1a.Item.complete = False Then
elements1a.Item.Click
If elements1a.Item.complete = True Then
MsgBox ("clicking the checkbox switched the completeness of the item to true")
End If
End If
MsgBox (elements1a.Item.className & " and " & elements1a.Item.Checked)
If elements1a.Item.className = "ng-valid ng-touched ng-dirty ng-valid-parse" Then
elements1a.Item.Checked = True
MsgBox (elements1a.Item.Checked)
'Element code when vba clicks the button:
'<form name="forms.addressContract" class="form __hor ng-scope ng-valid-maxlength ng-valid-pattern ng-invalid ng-invalid-required ng-valid-parse ng-pristine" novalidate="" ng-class="{'force-show-errors': isSubmitted}" ng-submit="submitAddress()">
'Element code when an actual user actually clicks the button
'<form name="forms.addressContract" class="form __hor ng-scope ng-valid-maxlength ng-valid-pattern ng-invalid ng-invalid-required ng-valid-parse ng-dirty" novalidate="" ng-class="{'force-show-errors': isSubmitted}" ng-submit="submitAddress()">
End If
Next

'the vba command for the not user-clicked checkbox
Set elements1b = ie.document.getElementsByClassName("ng-pristine ng-valid ng-touched")
MsgBox (element1b)
For Each Item In elements1b
If elements1b.Item.className = "ng-pristine ng-valid ng-touched" Then
elements1b.Item.Checked = True
MsgBox (elements1b.Item.Checked)
'<form name="forms.addressContract" class="form __hor ng-scope ng-valid-maxlength ng-valid-pattern ng-invalid ng-invalid-required ng-valid-parse ng-pristine" novalidate="" ng-class="{'force-show-errors': isSubmitted}" ng-submit="submitAddress()">
'<form name="forms.addressContract" class="form __hor ng-scope ng-valid-maxlength ng-valid-pattern ng-invalid ng-invalid-required ng-valid-parse ng-dirty" novalidate="" ng-class="{'force-show-errors': isSubmitted}" ng-submit="submitAddress()">

End If
Next

Answer Source

For those stumbling upon the same problem, this did the trick:

'1. Mark checkbox
    'MsgBox ("engage filling")
    Set elements1e = ie.document.getElementById("permission")
    'MsgBox (elements1e.Name & " " & elements1e.className & elements1e.placeholder)
    elements1e.Focus
    elements1e.Click
'    MsgBox ("cLICKED")

I am not sure why it is possible to .focus and .click on this element and not on others. I think it has to do with the type of object it is within the html code of the site itself.