excelvba&;IE 11-在下拉列表中选择值后无法刷新页面
我想知道世界汇兑公司提供的一对货币的汇率。我想更改网页左上角“发送自”下拉列表中的值。() 我无法使用excelvba&;IE 11-在下拉列表中选择值后无法刷新页面,excel,vba,internet-explorer,web-scraping,Excel,Vba,Internet Explorer,Web Scraping,我想知道世界汇兑公司提供的一对货币的汇率。我想更改网页左上角“发送自”下拉列表中的值。() 我无法使用选择下拉选项。Selected=True或。请单击,以便使用。SelectedIndex。选择该值后,我无法触发刷新页面的更改事件。如果有人能帮我解决这个问题就太好了 导航到该页面的代码: Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True ie.navigate "https://www.worldre
选择下拉选项。Selected=True
或。请单击,以便使用。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
选择选项的功能(用于上述代码):
函数Find_Select_选项(selectElement为HTMLSelectElement,optionText为String)为整数
作为整数的Dim i
查找\选择\选项=-1
i=0
而i
编辑#1:包含相关元素的HTML代码段是
<select id="selectFrom" data-track-field-name="from country" data-track-event="change">...</select>
。。。
试试看,这对我有好处。有时使用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设置为对象:设置IE=CreateObject(“InternetExplorer.Application”)
与IE
.Visible=True
.导航“https://www.worldremit.com/en/South-Africa"
'等待页面加载
忙时或重新启动状态4
Application.Wait(现在()+TimeValue(“00:00:01”))
多芬特
温德
'使用JQuery根据ID查找元素,然后使所选属性为true
'完成后,在jQuery中调用change事件
.document.parentWindow.execScript“$('#selectFrom选项:contains(德语)).prop('selected','True')”
.document.parentWindow.execScript“$(”#selectFrom选项:contains(德语)).change()
以
端接头
显然FireEvent
与IE 11不太兼容,因此需要使用CreatEvent
+initEvent
+dispatchEvent
下面的工作代码段:
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
尝试对下拉元素Focus
,触发“OnClick”事件,并对另一个元素Focus
进行操作。尝试对另一个元素Focus
+OnClick
+Focus
进行操作,但无效。另外,当我在即时窗口中检查元素时,元素.OnClick
和元素.OnChange
属性将返回Null
。非常感谢!找到了另一种方法,但肯定也会检查这一点:)另外,您的代码似乎也可以与旧版本的IE一起使用。应该更快,代码也更少。学习jQuery(如果您还不知道的话)将为您节省一些迭代元素和处理IE对象中不存在的事件的麻烦。您下面的解决方案是正确的,您需要创建一个新的事件“更改”,然后启动它。确实如此。将尝试学习一些jQuery技能。再次感谢。
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