问题
I am trying to fill one input box in web page which actually trigger events results in auto update some hidden fields.
code behind web page
<INPUT onchange=iCFieldChanged(this); tabIndex=1017 onkeypress="AllowOnly(event, '[()0-9-+]','');" onfocus="tbInitialValue=this.value;g_fFieldValue = this.value;g_fFieldId = this.id;" onblur="if(tbInitialValue!=this.value && Page_IsValid){executingPostBack=true;__doPostBack('dnn:ctr366:ClaimPhysicianInformation:_ctl2:Datapanel:_ctl0:_ctl5:ClaimRecipient:ds_ClaimRecipient:mb_ds_ClaimRecipient', '');}g_fFieldId = null;" id=dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient onpaste="return true;" style="TEXT-ALIGN: right" maxLength=10 size=11 value=9468381692 name=dnn:ctr366:ClaimPhysicianInformation:_ctl2:Datapanel:_ctl0:_ctl5:ClaimRecipient:ds_ClaimRecipient:mb_ds_ClaimRecipient>
There are 5 event listeners on the first input tag line of the code above - onblur
,onkeypress
,onfocus
,onchange
,onpaste
. I can able to pass value to the input box but associated events not get released.
code tried (no errors but events not dispatched)
ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient").Value = memberId
'tried with all fire event combinations but nothing works
ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient").FireEvent ("onkeypress")
also tried to dispatch events but its showing error
Set srchbtn = ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient")
srchbtn.Value = memberId
'getting error in next line
Set event_onChange = ie.Document.createEvent("HTMLEvents")
event_onChange.initEvent "keypress", True, False
srchbtn.dispatchEvent event_onChange
also tried keypress events like below
Set objEvent = IE.Document.createEvent("keyboardEvent")
objEvent.initEvent "keypress", True, False
IE.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient")(0).dispatchEvent objEvent
but same error appears
error: Run time error '438' : object doesn't support this property or method
since its an secured web page I cant share the url for reproducing the code please guide me on further proceedings
回答1:
Does your event of input can be fired correctly in IE without VBA? I tried to dispatch the onchange
event and it worked. I followed the way in this article. You could refer to my simple sample below:
Input box: <input name="q" onchange="alert('aaa');"/>
VBA code:
Sub LOADIE()
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
ieA.navigate "http://somewebsite"
Do Until ieA.readyState = 4
DoEvents
Loop
Set doc = ieA.Document
Set Search = doc.getElementsByName("q")(0)
Search.Value = "VBA"
Dim event_onChange As Object
Set event_onChange = ieA.Document.createEvent("HTMLEvents")
event_onChange.initEvent "change", True, False
Search.dispatchEvent event_onChange
ieA.Quit
Set ieA = Nothing
End Sub
The function above also worked with keypress
event. About the error you got, you could check this thread for more information.
回答2:
This might help others who are facing same issue
set srchbtn = ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient")
srchbtn.Focus
srchbtn.Value = memberId
srchbtn.Blur
来源:https://stackoverflow.com/questions/57561495/excel-vba-to-trigger-events-in-web-page-once-input-box-get-filled-with-data