【问题标题】:Excel VBA & IE 11 - Unable to refresh page after selecting value in a dropdownExcel VBA & IE 11 - 在下拉列表中选择值后无法刷新页面
【发布时间】: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.OnClickElement.OnChange 属性返回 Null

标签: excel vba internet-explorer web-scraping


【解决方案1】:

试试这个,它对我有用。有时使用 jQuery 会更容易一些,尤其是当页面也像这里一样使用 jQuery 时。

您可以通过 IE 的 execScript 函数来使用 jQuery。见下文:

 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

【讨论】:

  • 非常感谢!找到了另一种方法,但肯定也会检查一下 :) 另外,您的代码似乎也适用于旧版本的 IE。
  • 应该更快,代码也更少。学习 jQuery(如果你还不知道的话)会让你在迭代元素和处理不作为 IE 对象的一部分存在的事件时省去一些麻烦。您下面的解决方案是正确的,您需要创建一个新事件“更改”然后触发它。
  • 确实如此。将尝试学习一些 jQuery 技能。再次感谢。
【解决方案2】:

显然FireEvent 不能很好地与 IE 11 一起使用,因此需要使用 CreatEvent + initEvent + dispatchEvent

下面的工作代码sn-p:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多