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
652 views
in Technique[技术] by (71.8m points)

html - Selecting Dropdown list in IE with VBA

I have been trying to select a drop down list in a web page using VBA, which I'm new to. In HTML the drop down menu is stated as Button and not Select. Here is the HTML code:

<span class='btn-group'>
  <button id='str_listing-btn' name='str_listing-btn' type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown' data-value='For Sale'>
    For Sale
    <span class='caret' style='margin-left:5px;'>
    </span>
  </button>
  <ul class='dropdown-menu wptk_crud_dropdown' id='str_listing' data-value='str_listing' role='menu'>
    <li>
      <a href='#' data-value='For Sale'>For Sale</a>
    </li>
    <li>
      <a href='#' data-value='For Rent'>For Rent</a>
    </li>
    <li>
      <a href='#' data-value='Wanted To Buy'>Wanted To Buy</a>
    </li>
    <li>
      <a href='#' data-value='Wanted To Rent'>Wanted To Rent</a>
    </li>
  </ul>
</span>

I have tried a few VBA codes to select one of the options. Below is the latest code that I have used but after running it nothing seems to happen to the drop down menu:

Private Sub InsertPropwall_Click()

Dim objIE1 As Object
objIE1.navigate ("http://www.propwall.my/classifieds/post_ad?action=add")

Do
DoEvents
Loop Until objIE1.ReadyState = 4

objIE1.Document.getElementById("str_listing-btn").Value = "For Rent"

Do
DoEvents
Loop Until objIE1.ReadyState = 4

End Sub
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The dropdown isn't a dropdown (as you've mentioned). Instead, it's an unordered list with a number of links as clickable options. With that said, the For Rent option isn't contained in str_listing-btn. Notice how that tag closes before any of the options are available. Instead, it's contained in the list below it.

Since I couldn't get into your link without creating an account, I tested out code to select the For Rent option on the main page. Take a look at my code, test it to see if it does what you need, then try to accommodate it into your code. Let us know if you need additional help.

Sub NavigateIt()
    Dim oIE As Object

    Set oIE = CreateObject("InternetExplorer.Application")
    oIE.navigate ("http://www.propwall.my/classifieds")
    oIE.Visible = True

    Do
        DoEvents
    Loop Until oIE.ReadyState = 4

    Set AvailableLinks = oIE.document.getelementbyid("list-listing").getelementsbytagname("a")

    For Each cLink In AvailableLinks
        If cLink.innerhtml = "For Rent" Then
            cLink.Click
        End If
    Next cLink

End Sub

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

...