问题描述
我是VBA的新手,我必须从特定网站的Internet Explorer页面之一的下拉菜单中选择选项2.
I am new to VBA and i have to select option 2 from a dropdown menu in one of the internet explorer pages of a specific website.
这是网页html代码的快照.
Here is a snapshot of the html code of the webpage.
我无法找到一种在VBA中访问下拉选项的方法.我尝试使用它,但是ist似乎没有针对我想要的元素.
I cannot figure out a way of accessing the dropdown option in VBA.I tried using this but ist doesn't seem to target the element i want.
Set objShell3 = CreateObject("Shell.Application")
IE_count = objShell3.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell3.Windows(x).document.Location
my_title = objShell3.Windows(x).document.Title
MsgBox ("The title of this page is: " & my_title)
If my_title Like "Export to Excel" & "*" Then
Set ie3 = objShell3.Windows(x).document
my_title3 = ie3.Title
MsgBox ("The title of Export to Excel is: " & my_title3)
Exit For
Else
End If
Next
For Each element In ie3.getElementsByTagName("a")
If element.innerText = "Option2" Then
element.Click
Exit For
Else
End If
Next
是否有特定的语法可访问vba中的下拉菜单选项?
Is there any specific syntax to access the options of a dropdown menu in vba?
谢谢:)
推荐答案
URL确实会有所帮助,但您要查找的内容必须在该select标记元素中.
A url would be helpful indeed but what you are looking for must be in that select tag element.
HTML名称属性并不总是唯一的,但是假设这是第一个使用此名称的属性,您可以通过以下方式将select元素设置为对象:
HTML name attributes are not always unique but assuming that's the first one with this name you can set the select element into an object this way:
Set objSelect = ie3.getElementsByName("selectedHostId")(0)
如果展开该元素,您将看到它具有带有选项标签的子元素.选项标签具有值和内部文本,它们并不总是相同的.您必须使用该值.
If you expand that element, you'll see that it has child elements with option tag.Option tags have value and also innertext which are not always the same. You have to use the value.
objSelect.Value = "ValeOfOptionElementYouNeed"
该下拉菜单包含一个onChange事件.您可能需要触发"它以模仿手动选择:
The dropdown has an onChange event. You might need to "fire" it in order to imitate a manual selection:
objSelect.FireEvent "onChange"
这篇关于如何从VBA的下拉菜单访问选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!