Excel VBA & IE 11 - Unable to refresh page after selecting value in a dropdown

前端 未结 2 1588
星月不相逢
星月不相逢 2021-01-23 22:13

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 corne

相关标签:
2条回答
  • 2021-01-23 22:38

    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:

     Public Sub test()
        Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
    
        With IE
            .Visible = True
            .navigate "https://www.worldremit.com/en/South-Africa"
            'Wait for the page to load
            While .busy Or .readyState <> 4
                Application.Wait (Now() + TimeValue("00:00:01"))
                DoEvents
            Wend
            'Use JQuery to find the element based on ID, then make the Selected property true
            'Once that is done, call the change event in jQuery
            .document.parentWindow.execScript "$('#selectFrom option:contains(Germany)').prop('selected','True')"
            .document.parentWindow.execScript "$('#selectFrom option:contains(Germany)').change()"
        End With
    End Sub
    
    0 讨论(0)
  • 2021-01-23 22:40

    Apparently FireEvent doesn't work all that well with IE 11 so need to use CreatEvent + initEvent + dispatchEvent

    Working code snippet below:

    Dim fromSelect As HTMLSelectElement
    Dim evt As Object
    
    Set evt = HTMLdoc.createEvent("HTMLEvents")
    evt.initEvent "change", True, False
    Set fromSelect = HTMLdoc.getElementById("selectFrom")
    optionIndex = Find_Select_Option(fromSelect, "Germany")
    If optionIndex >= 0 Then
        fromSelect.selectedIndex = optionIndex
        fromSelect.dispatchEvent evt
    End If
    
    0 讨论(0)
提交回复
热议问题