在Internet Explorer的组合框中选择值使用Excel VBA

时间:2018-11-24 21:07:38

标签: excel vba internet-explorer web ui-automation

See this screenshot, is this the one to click on drop down HTML Code after selecting Metrics-1 from Drop down menu我正在尝试使用vba从Internet Explorer的组合框中选择值。打开网站时,组合框具有默认值。我想从组合框中选择最后一个值。我检查了它的元素,发现了唯一的ID。浏览器的检查元素或索引号中没有选项列表,但是组合框显示了8个列表。

我尝试了getElementById("Unique_Id").innerText = "Last_value",它在组合框中给出了值。但是问题是当我使用鼠标选择相同的对象时,下一个组合框将刷新并根据当前的组合框值选择给出相关的结果。通过vba执行相同操作后,下一个组合框没有刷新。它显示了在combox中选择的默认值的结果,而不是我使用innerText给出的值。

请注意。我已经尝试过.onfocus.click.selectedindex来获取所选值的结果,但是没有任何效果。

下拉:

•<div id="reportsDropDownContainer" class="col-lg-4 col-md-4 col-sm-4 col-xs-4"›
•	<div class="form-group"›
<label id="reportsDropDownLabel" class="control-laber> Presets</label>
•	<div class="Select has-value Select--single">
•	<div class="Select-contror>
•	<div class="Select-multi-value-wrapper" id="react¬select-4—value"›
•	<div class="Select-value"›
<span class="Select-value-label" role="option" aria-selected="true" id="react-select-4--value-item">Activity History</span> == $0
</div>
<div aria-expanded="false" aria-owns aria-activedescendant="react-select-4--value" aria-disabled="false" class="Select-input" role= "combobox" tabindex="0" style="border: Opx; width: ipx; display: inline-block;"></div>
</div>

Sub ABC()

Dim IE As SHDocVw.InternetExplorer
Dim HTMLdoc As MSHTML.HTMLDocument
Dim HTMLInputs As MSHTML.IHTMLElementCollection
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLTags As MSHTML.IHTMLElementCollection
Dim HTMLTag As MSHTML.IHTMLElement
Dim e As Object

Set IE = New InternetExplorerMedium

IE.Visible = True
IE.navigate "https://abc.domain.com"
Do
    DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Application.Wait (Now + TimeValue("00:02:00"))  'After ready state, takes time to load the some contents.

Set HTMLdoc = IE.document
Set HTMLTags = HTMLdoc.getElementsByTagName("a")

For Each HTMLTag In HTMLTags
If HTMLTag.getAttribute("href") = "/site/xyz/Reports" Then
HTMLTag.Click
Exit For
End If
Next

Application.Wait (Now + TimeValue("00:00:02"))

IE.document.getElementById("react-select-4--value-item").Item.Value = "Metrics-1"  'It's not working. Showing run time error
IE.document.getElementById("react-select-4--value-item").Value = "Metrics-1"    'It's not working. Showing run time error
IE.document.getElementById("react-select-4--value-item").selectedIndex = 8    'It 's not working. Showing run time error


Set HTMLInput = HTMLdoc.getElementById("react-select-4--value-item")
HTMLInput.innerText = "Metrics-1"
HTMLInput.Focus
HTMLInput.FireEvent "onchange"
HTMLInput.Click

'使用aria-activedescedant以另一种方式进行了相同的尝试。

Set HTMLInputs = HTMLdoc.getElementsByTagName("div")
For Each HTMLInput In HTMLInputs

If HTMLInput.className = "Select-input" And HTMLInput.role = "combobox" And HTMLInput.getAttribute("aria-activedescendant") = "react-select-4--value" Then
Debug.Print HTMLInput.getAttribute("aria-activedescendant")
HTMLInput.getAttribute("aria-activedescendant").Value = "react-select-4--Option-10"
Debug.Print HTMLInput.getAttribute("aria-activedescendant")

End If
Next
HTMLInput.getElementById("react-select-4--value-item").setAttribute.Value = "Metrics-1"

End Sub

0 个答案:

没有答案