使用VBA在IE中选择下拉列表 [英] Selecting Dropdown list in IE with VBA

查看:301
本文介绍了使用VBA在IE中选择下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用VBA来选择网页中的下拉列表,这是我的新手。在HTML中,下拉菜单显示为按钮,而不是选择。这里是HTML代码:

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>

我尝试了几个VBA代码来选择其中一个选项。下面是我用过的最新代码,但运行后没有任何事情发生在下拉菜单中:

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


推荐答案

下拉列表不是下拉菜单(如您所述)。相反,它是一个无序列表,其中有多个链接作为可点击的选项。这就是说, For Rent 选项不包含在 str_listing-btn 中。请注意,在任何选项可用之前,该标记是如何关闭的。相反,它包含在下面的列表中。

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

这篇关于使用VBA在IE中选择下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆