Home > Mobile >  Firing events associated with input box
Firing events associated with input box

Time:04-03

I am having trouble firing events associated with an input box. These are Event listeners in the "Events" window of "DOM Explorer" when I inspect the relevant text box. They are however not listed in the HTML of the input box itself.

Of the several events listeners listed (e.g. "input", "blur", "paste") they all fire the same javascript function "x". Whilst my code does insert the text, the field does not validate on pressing the submit button and highlights red. A manual entry or a cut and paste will solve this.

I tried .fireEvent ; .dispatchEvent and even attempted to call the javascript directly. It is a public website and the address is in the code. I include below the code to fill in the first 3 fields.

Below is the HTML of the field:

<input  aria-labelledby="QuestionId_r57b9be42eed24e63b4d2af31c178f530 QuestionInfo_r57b9be42eed24e63b4d2af31c178f530" spellcheck="true" maxlength="4000" placeholder="Enter your answer">

The screenshot of the DOM explorer window: enter image description here

Private Sub Test()
myAddress = "https://forms.office.com/Pages/ResponsePage.aspx?id=q4GdsF1ZzU2rJGpWVuGNiROj17Ac1mtIt6V80jIX_PFUMEs1VTZaQ0VRV1NNNVI4Vzg3V0RMOE83TS4u"

'create new instance of IE.
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate myAddress

'wait for loading
Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
  
WasteTime (0.1) 'Custom function to pause for 0.1 sec

Dim myData(1 To 3, 1 To 3) As String

myData(1, 1) = "Member Surname"
myData(2, 1) = "QuestionId_r57b9be42eed24e63b4d2af31c178f530"
myData(3, 1) = "Chowdhary"

myData(1, 2) = "Member Forename"
myData(2, 2) = "QuestionId_rb88bf6d86f284dbaa14e5f3f27ee9f5b"
myData(3, 2) = "Saqib"

myData(1, 3) = "Member DOB"
myData(2, 3) = "QuestionId_r38d6e5656a6b4fbf86f586d10cbd8cb6"
myData(3, 3) = "19/04/1971"

Set objItem = IE.document.getElementsByTagName("input")
x = 0
Do While x < objItem.Length
    Set ele = objItem(x)
    mySearch = ele.outerHTML
    For c = 1 To UBound(myData, 2)
        If InStr(mySearch, myData(2, c)) > 0 Then
            ele.Focus
            ele.Value = myData(3, c)                
            Set event_onChange = IE.document.createEvent("HTMLEvents")
            event_onChange.initEvent "paste", True, False
            ele.dispatchEvent event_onChange 'This does not fire event                
            'ele.FireEvent ("onpaste") '- This did not work either
            'IE.document.parentWindow.execScript code:="x(c)" ' Tried to directly call the javascript function                                    
            Exit For
        End If
    Next c
    x = x   1
Loop

End sub

CodePudding user response:

The event in the search box can be fired with SendKeys. You can simulate user input using SendKeys to set value of the input boxes then click Submit to submit the form.

You can refer to the code snippet below. I have tested and it works:

Set objItem = IE.document.getElementsByTagName("input")(0)
objItem.Focus
SendKeys ("Chowdhary")
Application.Wait (Now   TimeValue("00:00:02"))
IE.document.getElementsByTagName("button")(4).Click

CodePudding user response:

So I did finally get the code to work based on the same .initEvent and .dispatchEvent methods. I just needed to introduce a short 0.1 sec pause (using a custom "Wastetime" function) to allow the element to fully load. For interest the changed relevent code snippet is:

Set objItem = IE.Document.getElementsByTagName("input")
x = 0
Do While x < objItem.Length
    Set ele = objItem(x)
    mySearch = ele.outerHTML ' Could just enumerate the input indexes in order
    For c = 1 To UBound(myInternalArray, 2)
        If InStr(mySearch, myInternalArray(2, c)) > 0 Then
            If myInternalArray(3, c) = "click" Then
                ele.Click
                WasteTime (0.1) 'Time for form to expand
                Exit For
            Else
                ele.Value = myInternalArray(3, c)
                ele.Focus
                Set event_onInput = IE.Document.createEvent("HTMLEvents")
                event_onInput.initEvent "input", True, False
                ele.dispatchEvent event_onInput                
                Exit For
            End If
        End If
    Next c
    x = x   1
Loop
  • Related