VBA:填写选择框后如何获取页面更新

问题描述 投票:0回答:1

我正在尝试使用此页面上的完整匹配表https://stats.nba.com/player/2544/head-to-head/?Season=2018-19&SeasonType=Regular%20Season到目前为止,这就是我所拥有的:

Option Explicit
Sub BrowsetoSite()

Dim IE As Object

Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True
IE.navigate "https://stats.nba.com/player/2544/head-to-head/?Season=2018-19&SeasonType=Regular%20Season"


Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop


IE.Document.getElementsByClassName("stats-table-pagination__select")(0).Value = "string:All"
On Error Resume Next
IE.Document.getElementsByClassName("stats-table-pagination__select")(0).Focus.FireEvent ("onchange")


End Sub

这将页面选择填充为“全部”,但是页面上载的方式与您在网站上单击该选项不同。我该如何解决?

这是所选内容的HTML代码:

<select class="stats-table-pagination__select ng-valid ng-not-empty ng-touched ng-dirty ng-valid-parse" ng-model="vm.data.page" ng-options="p for p in vm.data.pages" aria-invalid="false">
<option label="All" value="string:All">All</option>
<option label="1" value="number:1" selected="selected">1</option>
<option label="2" value="number:2">2</option>
<option label="3" value="number:3">3</option>
<option label="4" value="number:4">4</option>
<option label="5" value="number:5">5</option>
<option label="6" value="number:6">6</option>
<option label="7" value="number:7">7</option>
</select>
excel vba internet-explorer web-scraping
1个回答
0
投票

您可以使用以下代码。 FireEvent不适用于大多数现代页面。您必须使用dispatchEvent,并且只有在事先设置事件并通过initEvent对其进行初始化的情况下,才可以调度事件。为此,我使用下面的专用子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
© www.soinside.com 2019 - 2024. All rights reserved.