VBA IE Automation issue with Angularjs input text

JDLd500

New Member
Joined
Jul 30, 2019
Messages
4
Hi

I am automating the inputting of text and date/time fields using VBA and IE. The site concerned has AngularJS java scripts running behind the HTML.

I have triggered events to handle the date and time picker by clicking on buttons navigating the calendar if the month/year does not match what I want etc. and the updates work great.

However I have an issue with simple plain text fields on the page. I can:-
  • use the getelementbyid("text0").value = "xxxx" and .innertext = "xxxxx" to populate the input field on the web page (so I can see what I expect to see)
  • use the.classname = "xxx" to change the class (using DOM I can see that when I work manually on the page the class name changes to indicate that the field is "dirty" which the java must use to tell that a field has changed)
  • click the save button automatically

But I cannot get the necessary scripts to run so that the variable data that I have added is recognised and is actually saved (when I recall the record the text data is as it was).

The following is the HTML code:

HTML:
<input type="text" ng-model="feedbackItem.textValue" id="text0" ng-disabled="!feedbackIsEditable || feedbackItem.notApplicable" maxlength="255" class="ng-valid ng-valid-maxlength ng-dirty ng-valid-parse ng-touched" name="" tabindex="0" aria-disabled="false" aria-invalid="false" style="">

There are the following events associated with the field - blur, change, compositionend, compositionstart, input

I have tried to use fireevent to trigger "blur" "change" and "input" but get an error message in debug saying object required.

the code I am using is as follows (I have kept the commented out lines so that you can see the variations I have tried and have failed:

IE.document.getElementById("text0").Focus = True
IE.document.getElementById("text0").Click
strSub = Left("text to enter", Len("text to enter") - 1)
strKey = Right("text to enter", 1)


IE.document.getElementById("text0").Value = strSub
Application.SendKeys strKey
'IE.document.getElementById("text0").className = "ng-valid ng-valid-maxlength ng-dirty ng-valid-parse ng-touched"
'IE.document.getElementById("text0").Value = "text to enter"
'IE.document.getElementById("text0").innerText = "text to enter"
'IE.document.getElementById("text0").className = "ng-valid ng-valid-maxlength ng-dirty ng-valid-parse ng-touched"
'IE.document.getElementById("text0").feedbackItem.textValue = "text to enter"
IE.document.getElementById("text0").className = "ng-valid ng-valid-maxlength ng-dirty ng-valid-parse ng-touched"
'Elements.FireEvent ("change")

IE.document.getElementById("text0").Focus = False
IE.document.getElementById("date1").Focus = True 'this is the next field to populate


I am assuming that I need to either trigger the JS event(s) and/or somehow change the value of "feedbackItem.textValue"?

Anyone see the obvious mistake I'm making or step(s) I've missed?

Thanks in advance
John
 
In IE's DOM Explorer there is a pane on the right with tabs for Styles, Computed, Layout, Events, Changes. Click Events and it shows the events, if any, for the selected element:

DOM Explorer Events.PNG

Those are the events I'm talking about. In your case it isn't enough to just set the element's value property with your text string; you also have to send the appropriate events. The events you typically need to emulate for text input elements are keydown, keyup, mouseup, mousedown, input and change. Sometimes you have to input the text string character by character, sending the appropriate event(s) for each character - see my example code above.
 
  • Like
Reactions: kyu
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
In IE's DOM Explorer there is a pane on the right with tabs for Styles, Computed, Layout, Events, Changes. Click Events and it shows the events, if any, for the selected element:

View attachment 3717

Those are the events I'm talking about. In your case it isn't enough to just set the element's value property with your text string; you also have to send the appropriate events. The events you typically need to emulate for text input elements are keydown, keyup, mouseup, mousedown, input and change. Sometimes you have to input the text string character by character, sending the appropriate event(s) for each character - see my example code above.

John, thank you so much for helping.
I clicked on 'Events' and I see 4 values. I will attach a picture.

What should I do from here?
 

Attachments

  • Capture.PNG
    Capture.PNG
    6.6 KB · Views: 34
Upvote 0
In IE's DOM Explorer there is a pane on the right with tabs for Styles, Computed, Layout, Events, Changes. Click Events and it shows the events, if any, for the selected element:

View attachment 3717

Those are the events I'm talking about. In your case it isn't enough to just set the element's value property with your text string; you also have to send the appropriate events. The events you typically need to emulate for text input elements are keydown, keyup, mouseup, mousedown, input and change. Sometimes you have to input the text string character by character, sending the appropriate event(s) for each character - see my example code above.


John,

I have been trying out different things and got it to do what I wanted it to do.
I will try to figure out what exactly happened to make it work but thank you so much for your help.
 
Upvote 0
Glad you got it working. To answer your previous question, I would try element.focus (the focus method, not the event) before the loop, the keypress event and maybe the change event in the character-by-character loop, and the change event after the loop. Call DoEvents after every event.
 
  • Like
Reactions: kyu
Upvote 0
Glad you got it working. To answer your previous question, I would try element.focus (the focus method, not the event) before the loop, the keypress event and maybe the change event in the character-by-character loop, and the change event after the loop. Call DoEvents after every event.

After playing with the code for a few minutes, what I finally decided to use is the following form:
Set BirthDate = HTMLdoc.getElementById("birthdate_id")
BirthDate.Value = "1/2/1975"
BirthDate.dispatchEvent blurevent
Here, I chose blurevent because that was the first event when I clicked on Events tab you mentioned earlier.
I'm not sure if it has to be the first event or if it could be any one of the events, but I'm using the first event for all off the input boxes / dropdowns / etc.

Btw, I had tried using focus method but it didn't work for me. Thank you again for the help!
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top