【发布时间】:2018-12-07 16:21:21
【问题描述】:
我正在创建一个宏来从公共银行网站获取传真号码。
我已经编写了足够的代码来访问该站点,从下拉列表中进行选择,然后更改下拉列表中的选择。但是,当我使用 FireEvent ("onChange") 时,它不会触发网页更新。
我已经搜索了答案,但没有找到任何答案。
网址:https://www.atb.com/contact-us/Pages/branch-locator.aspx
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Sub test()
Dim ieExplorer As New InternetExplorerMedium
Dim ieField As Object
Dim ieSubmit As Object
Dim ieSelect As Object
Dim iebutton As Object
Dim buttCounter As Integer
Dim objOption As Object
Dim objCount As Integer
Dim ieForm As Object
Dim intRow As Long, faxNum As String
intRow = 2
With ieExplorer
.Visible = True
.Navigate "https://www.atb.com/contact-us/Pages/branch-locator.aspx"
Sleep 1000
Sleep 1000
Sleep 1000
Sleep 1000
Sleep 1000
Sleep 1000
Sleep 1000
Set ieSelect = .Document.getElementsByTagName("select")
Do While o < ieSelect.Length
If ieSelect(o).ID = "ba" Then
For Each i In ieSelect(o).Options
If i.Value <> "null" Then
ieSelect(o).Focus
i.Selected = True
ieSelect(o).FireEvent "onchange"
Set ieField = .Document.getElementsByTagName("p")
Do While x < ieField.Length
If InStr(ieField(x).innertext, "FAX") Then
Cells(intRow, "A").Value = i.Value
Cells(intRow, "B").Value = ieField(x).innertext
intRow = intRow + 1
End If
Loop
End If
Next
End If
o = o + 1
Loop
End With
End Sub
【问题讨论】:
-
为什么需要
onchange事件?看起来如果您选择一个下拉选项,网页几乎会立即更新。onchange是否应该检测到这种变化? -
您好,当我用鼠标手动执行此操作时,您是正确的,网站会立即更改。但是通过 VBA 编码时,它只是从下拉列表中选择分支,但不会触发网页更改。这就是我尝试使用 OnChange 事件的原因,因为这在其他网站上对我有用。然而,有了这个,它什么也没做。必须有某种形式的事件需要触发才能使网页发生变化。在这种情况下,onchange 不起作用。