Вы можете использовать следующий код. FireEvent не работает для большинства современных страниц. Вы должны использовать dispatchEvent , и вы можете отправлять событие, только если вы установили его ранее и инициализировали с помощью initEvent . Для этого я использую закрытый sub TriggerEvent () ниже.
Пожалуйста, прочитайте комментарии для получения дополнительной информации.
Код в заголовке макроса:
Option Explicit
'With the following Windows api function we can do breaks less than 1 second
'It works with Excel 32 bit and Excel 64 bit
#If Win64 Then
'For 64 bit systems
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
'For 32 bit systems
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Основной макрос:
Sub OpenAllPages()
Dim url As String
Dim browser As Object
Dim nodeDropdown As Object
url = "https://stats.nba.com/player/2544/head-to-head/?Season=2018-19&SeasonType=Regular%20Season"
'Initialize Internet Explorer, set visibility,
'call URL and wait until page is fully loaded
Set browser = CreateObject("internetexplorer.application")
browser.Visible = True
browser.navigate url
Do Until browser.ReadyState = 4: DoEvents: Loop
'Wait a little bit to load the table into the page from server
'(I have scheduled the break conservatively. You can try fewer seconds)
Sleep (5000)
'You must save the DOM objekt of the dropdown to an object
'variable to be able to trigger the change event later
Set nodeDropdown = browser.document.getElementsByClassName("stats-table-pagination__select")(0)
'Set the value to the object variable
nodeDropdown.Value = "string:All"
'Trigger the change event
Call TriggerEvent(browser.document, nodeDropdown, "change")
'Wait a little bit to open the table with all results
Sleep (500)
'Do here something you want with the whole table of results
End Sub
Макрос для запуска события html:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub