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
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