Excel VBA操作IE下拉列表问题:无法选中<li>标签下拉项
Fixing Custom
<ul>/<li> Dropdown Selection in Excel VBA for IE Hey Daniel, let's break down why your VBA code isn't selecting items from that custom dropdown. The core issue here is that this isn't a native HTML <select> element—it's a custom combobox built with <ul> and <li> tags. The standard methods you tried (using Select() on <li> or setting a Value property) only work for native form controls, not these custom UI components.
Why Your Previous Approaches Failed
- 方案一 (Approach 1):
<li>elements don't have a nativeSelect()method—that's a method reserved for<option>tags inside real<select>dropdowns. CallingobjCollection(i).Selectdoes nothing because the method doesn't exist for<li>elements. - 方案二 (Approach 2):
<li>elements don't have aValueproperty you can directly set, and your code didn't even reference a specific<li>element (thelivariable was undefined), so this approach was never going to work.
Working Solution: Simulate User Interaction
Custom dropdowns like this rely on user clicks to expand the menu and select items. We need to replicate that behavior in VBA:
- Locate the parent
<ul>dropdown container - Expand the dropdown (if it's closed by default) by clicking the
<ul>or its trigger element - Find the target
<li>item by its text content - Click the
<li>item to select it
Here's the code to implement this:
Sub SelectCustomDropdownItem() Dim IE As Object Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True IE.Navigate "YourTargetURLHere" ' Replace with your actual target URL ' Wait for the page to load completely Do While IE.Busy Or IE.ReadyState <> 4 DoEvents Loop ' Step 1: Locate the dropdown <ul> element Dim ulDropdown As Object Set ulDropdown = IE.document.getElementById("id-999") ' Wait for the dropdown element to be available (handles dynamic loading) Do While ulDropdown Is Nothing Set ulDropdown = IE.document.getElementById("id-999") DoEvents Loop ' Step 2: Expand the dropdown (skip if it's always open) ulDropdown.Click Application.Wait Now + TimeValue("00:00:01") ' Short wait for options to render ' Step 3: Find and click the target <li> item Dim liItem As Object For Each liItem In ulDropdown.getElementsByTagName("li") ' Match the item text (trim to handle extra whitespace) If Trim(liItem.innerText) = "Number" Then ' Click the item to select it liItem.Click ' Fallback: Trigger onclick event directly if click doesn't work ' liItem.FireEvent "onclick" Exit For End If Next liItem ' Optional cleanup ' IE.Quit ' Set IE = Nothing End Sub
Additional Tips
- Wait for dynamic content: If the dropdown options load after the initial page load, add a loop to wait for the
<li>items to exist before trying to select them. - Alternative event triggers: If
liItem.Clickdoesn't register, try usingFireEvent "onmousedown"—some custom controls listen for specific mouse events instead of the generic click. - Flexible text matching: Use
InStrinstead of exact matches if the<li>text has extra characters (e.g.,If InStr(liItem.innerText, "Number") > 0 Then).
内容的提问来源于stack exchange,提问作者Daniel Nikolov
相关产品推荐
相关产品推荐

