Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

html - How to access option from dropdown menu in VBA

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.

Here is a snapshot of the html code of the webpage. enter image description here

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

Is there any specific syntax to access the options of a dropdown menu in vba?

Thank you :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A url would be helpful indeed but what you are looking for must be in that select tag element.

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"

The dropdown has an onChange event. You might need to "fire" it in order to imitate a manual selection:

objSelect.FireEvent "onChange"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...