我試圖獲得WorldRemit爲一對貨幣提供的貨幣兌換率。我想更改網頁左上角的「發送自」下拉列表中的值。 (https://www.worldremit.com/en/South-Africa)Excel VBA和IE 11 - 在下拉列表中選擇值後無法刷新頁面
我無法使用.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
屬性選擇選項:
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
:
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
功能用於選擇的選項(在上面的代碼中使用)
編輯#1:包含相關元素的HTML片段是
<select id="selectFrom" data-track-field-name="from country" data-track-event="change">...</select>
先給下拉元素「焦點」,觸發「OnClick」事件,並給出另一個元素「焦點」。 –
在另一個元素上嘗試了'Focus' +'OnClick' +'Focus',但沒有奏效。 此外,當我在即時窗口中檢查它們時,下拉列表的'Element.OnClick'和'Element.OnChange'屬性返回'Null'。 – cattlesnake