How to detect a webpage is finish of updating triggered by JavaScript onchange?

rickweng

New Member
Joined
Sep 14, 2016
Messages
1
I would like to download a webpage data,which was triggered by JavaScript onchage function.
The problem is how to wait for the changeof webpage (triggered by JavaScript onchage function) completed updated before I can retrieve the data using my VBA code.

Webpage: http://www.cnyes.com/twstock/a_QFII9.aspx

Trigger by JavaScript onchange:
ie.document.getElementById("ctl00_ContentPlaceHolder1_D1")= "TSE"
ie.document.getElementById("ctl00_ContentPlaceHolder1_D3")= "2016-09-09"

MyVBA code in following just fetch the webpage data but won’t wait for thewebpage change completed triggered by JavaScript.
Can anyone help? Thank you.


Sub test()
Dim ie As InternetExplorer
Dim select1 As HTMLSelectElement
Dim select2 As HTMLSelectElement

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.cnyes.com/twstock/a_QFII9.aspx"
Do While ie.Busy Or ie.readyState < READYSTATE_COMPLETE
DoEvents
Loop


'==================================================================
'I set my value to select1 and select2
'Use FireEvent ("onchange") to trigger Javascript to update webpage
'==================================================================

Set select1 = ie.document.getElementById("ctl00_ContentPlaceHolder1_D1")
select1.Value = "TSE"

Set select2 = ie.document.getElementById("ctl00_ContentPlaceHolder1_D3")
select2.Value = "2016-09-09"

select1.FireEvent ("onchange") 'javascript to trigger change of TSE
select2.FireEvent ("onchange") 'javascript to trigger change of 2016-09-09


'==================================================================
'How can I wait for the above change of webpage completed updated?
'then I can proceed to retrieve the data using below VBA code?
'==================================================================


'Starging to download webpage data using VBA but it won't wait for the webpage change completed

Dim objHtml As Object
Dim target_tb As HTMLTable
Dim tbRow As HTMLTableRow
Dim tbCell As HTMLTableCell
Dim writeRange As Range


For Each objHtml In ie.document.getElementsByTagName("table")
If objHtml.className = "fLtBx" Then
Set target_tb = objHtml
End If
Next

If target_tb Is Nothing Then
MsgBox "Your finding table not exist"
Else

Set writeRange = Sheet1.Range("A1")
Sheet1.Cells.Clear

r = 0
For Each tbRow In target_tb.Rows
c = 0
For Each tbCell In tbRow.Cells
writeRange.Offset(r, c) = tbCell.innerText
c = c + 1
Next
r = r + 1
Next
End If
ie.Quit
Set ie = Nothing


End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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