使用 VBA 在 IE 中选择下拉列表 [英] Selecting Dropdown list in IE with VBA
问题描述
我一直在尝试使用 VBA 在网页中选择一个下拉列表,这是我的新手.在 HTML 中,下拉菜单表示为 Button 而不是 Select.这是 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
推荐答案
下拉列表不是下拉列表(正如您所提到的).相反,它是一个无序列表,其中包含许多链接作为可点击选项.话虽如此,str_listing-btn
中不包含 For Rent
选项.请注意在任何选项可用之前该标签是如何关闭的.相反,它包含在它下面的列表中.
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.
由于不创建帐户就无法进入您的链接,因此我测试了代码以选择主页上的For Rent
选项.查看我的代码,测试它是否满足您的需求,然后尝试将其纳入您的代码中.如果您需要其他帮助,请告诉我们.
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屋!