与输入框关联的触发事件

Firing events associated with input box

我无法触发与输入框关联的事件。当我检查相关文本框时,这些是“DOM Explorer”的“事件”window 中的事件侦听器。但是,它们未在输入框本身的 HTML 中列出。

在列出的几个事件侦听器中(例如“输入”、“模糊”、“粘贴”),它们都触发相同的 javascript 函数“x”。虽然我的代码确实插入了文本,但该字段在按下提交按钮时未验证并突出显示为红色。手动输入或剪切和粘贴将解决此问题。

我试过 .fireEvent ; .dispatchEvent 甚至试图直接调用 javascript。这是一个 public 网站,地址在代码中。我在下面包含了用于填写前 3 个字段的代码。

下面是字段的HTML:

<input class="office-form-question-textbox office-form-textfield-input form-control office-form-theme-focus-border border-no-radius" aria-labelledby="QuestionId_r57b9be42eed24e63b4d2af31c178f530 QuestionInfo_r57b9be42eed24e63b4d2af31c178f530" spellcheck="true" maxlength="4000" placeholder="Enter your answer">

DOM 浏览器 window 的截图:

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

搜索框中的事件可以用 SendKeys 触发。您可以使用 SendKeys 模拟用户输入来设置输入框的值,然后单击提交以提交表单。

您可以参考下面的代码片段。我已经测试过并且有效:

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

所以我终于让代码基于相同的 .initEvent.dispatchEvent 方法工作。我只需要引入一个短暂的 0.1 秒暂停(使用自定义“Wastetime”函数)以允许元素完全加载。有趣的是,更改后的相关代码片段是:

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