I'm trying to get the currency exchange rate offered by WorldRemit for a pair of currencies. I want to change the value in the 'Send From' dropdown list on the top left corner of the webpage. (https://www.worldremit.com/en/South-Africa)
I wasn't able to select the dropdown option using .Selected = True
or .Click
so have used .SelectedIndex
. After selecting the value, I'm not able to trigger the change event that refreshes the page. Would be great if someone can help me figure this out.
Code for navigating to the page:
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
Code for selecting option using .SelectedIndex
property:
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 for selecting option (used in the code above):
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
Edit #1 : The HTML snippet containing the element in question is
<select id="selectFrom" data-track-field-name="from country" data-track-event="change">...</select>
Apparently
FireEvent
doesn't work all that well with IE 11 so need to useCreatEvent
+initEvent
+dispatchEvent
Working code snippet below:
Give this a try, it's working on my end. Sometimes using jQuery is a bit easier, especially when the page also uses jQuery as it does here.
You can use jQuery by using the execScript function of IE. See below: