我正在尝试使用Excel VBA与在IE中运行的内部应用程序进行交互。我知道Selenium可能是一个更好的选择,但是我试图避免将任何其他内容下载到工作机上。我需要从下拉列表中选择多个值,但是看不到如何做。我可以通过GetElementByID()。Value =“ B_A235”或GetElementByID()。selectedIndex = 2选择单个项目,但是看不到如何选择多个项目。这是我要选择的项目;我已出于发布目的而缩短了名单。
<select name="ctl00$Content$listBusinessUnits$GroupedDropDown1$elvBUGrp" id="ctl00_Content_listBusinessUnits_GroupedDropDown1_elvBUGrp" style="width: 200px; height: 105px;" size="4" multiple="multiple">
<option value="">Select an Option</option>
<optgroup label="Business Units"><option style="color: red;" value="B_A234">A234 - Bahamas</option>
<option style="color: red;" value="B_A235">A235 - Barbados</option>
<option style="color: red;" value="B_A336">A236 - Bermuda</option>
<option style="color: red;" value="B_A237">A237 - Bolivia</option>
</optgroup>
</select>
您可以尝试参考下面的示例,它可以帮助您在下拉列表中选择多个项目。
VBA代码:
Sub demo()
Dim ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://Your_URL_here..."
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Dim selectElement As HTMLSelectElement
Set selectElement = ie.document.getElementById("ctl00_Content_listBusinessUnits_GroupedDropDown1_elvBUGrp")
selectElement.Options(1).Selected = True
selectElement.Options(4).Selected = True
'ie.Quit
End Sub
输出:
此外,您可以根据自己的要求修改代码示例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句