【发布时间】:2016-10-03 08:35:37
【问题描述】:
我正在尝试获取 WorldRemit 为一对货币提供的货币汇率。我想更改网页左上角“发送自”下拉列表中的值。 (https://www.worldremit.com/en/South-Africa)
我无法使用.Selected = True 或.Click 选择下拉选项,所以我使用了.SelectedIndex。选择值后,我无法触发刷新页面的更改事件。如果有人能帮我解决这个问题,那就太好了。
导航到页面的代码:
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://www.worldremit.com/en/South-Africa"
While ie.busy
DoEvents
Wend
Set HTMLdoc = ie.document
使用.SelectedIndex属性选择选项的代码:
Dim fromSelect As HTMLSelectElement
Set fromSelect = HTMLdoc.getElementById("selectFrom")
optionIndex = Find_Select_Option(fromSelect, "Germany")
If optionIndex >= 0 Then
fromSelect.selectedIndex = optionIndex
fromSelect.FireEvent ("onchange") ' this doesn't work
End If
选择选项的功能(在上面的代码中使用):
Function Find_Select_Option(selectElement As HTMLSelectElement, optionText As String) As Integer
Dim i As Integer
Find_Select_Option = -1
i = 0
While i < selectElement.Options.Length And Find_Select_Option = -1
DoEvents
If LCase(Trim(selectElement.Item(i).Text)) = LCase(Trim(optionText)) Then Find_Select_Option = i
i = i + 1
Wend
End Function
编辑#1:包含相关元素的 HTML sn-p 是
<select id="selectFrom" data-track-field-name="from country" data-track-event="change">...</select>
【问题讨论】:
-
尝试给下拉元素
Focus,触发“OnClick”事件,然后给另一个元素Focus。 -
在另一个元素上尝试了
Focus+OnClick+Focus,但没有成功。此外,当我在即时窗口中检查它们时,下拉列表的Element.OnClick和Element.OnChange属性返回Null。
标签: excel vba internet-explorer web-scraping