You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在QTP/UFT V12.02中处理下拉框?含两项具体操作需求

Hey there! Let's walk through how to handle dropdown controls in QTP/UFT 12.02—specifically two common tasks: getting the number of options and retrieving their text content. I'll cover both standard dropdowns (recognized as WebList objects) and custom dropdowns (like those built with div/ul/li) since both scenarios pop up often.

1. 获取下拉框的选项数量

对于标准WebList控件

UFT has built-in methods to make this straightforward. Here are two reliable approaches:

方法一:直接读取"items count"属性

Use GetROProperty to fetch the pre-defined property that tells you how many options are in the dropdown:

Dim dropdown, optionCount
Set dropdown = WebList("name:=yourDropdownIdentifier") ' Replace with your dropdown's test object name
optionCount = dropdown.GetROProperty("items count")
MsgBox "Total options in dropdown: " & optionCount

方法二:通过GetItems数组获取长度

The GetItems method returns an array of all option texts. You can use UBound to get the last index, then add 1 (since VBScript arrays start at 0) to get the total count:

Dim dropdown, optionArray, optionCount
Set dropdown = WebList("name:=yourDropdownIdentifier")
optionArray = dropdown.GetItems()
optionCount = UBound(optionArray) + 1
MsgBox "Total options in dropdown: " & optionCount

对于自定义下拉框(非标准WebList)

If your dropdown is a custom one (e.g., made with <ul>/<li> elements instead of a native <select>), you'll need to use ChildObjects to locate all option elements:

Dim optionElements, optionCount
' Replace the parent object and property with your dropdown's structure
Set optionElements = Browser("YourBrowser").Page("YourPage").WebElement("tagname:=ul").ChildObjects("tagname:=li")
optionCount = optionElements.Count
MsgBox "Total options in custom dropdown: " & optionCount

2. 获取下拉框的选项文本内容

对于标准WebList控件

获取所有选项文本

Use GetItems to get an array of all option texts, then loop through it to access each one:

Dim dropdown, optionArray, i
Set dropdown = WebList("name:=yourDropdownIdentifier")
optionArray = dropdown.GetItems()

' Loop through each option to print or use the text
For i = 0 To UBound(optionArray)
    MsgBox "Option " & (i + 1) & ": " & optionArray(i)
Next

获取当前选中的选项文本

If you only need the text of the selected option, use either GetSelectedItem or GetROProperty("value"):

Dim dropdown, selectedText
Set dropdown = WebList("name:=yourDropdownIdentifier")

' Method 1: Use GetSelectedItem
selectedText = dropdown.GetSelectedItem()

' Method 2: Use GetROProperty
selectedText = dropdown.GetROProperty("value")

MsgBox "Currently selected option: " & selectedText

对于自定义下拉框(非标准WebList)

Loop through the ChildObjects collection we created earlier, and use GetROProperty("innertext") to get each option's text:

Dim optionElements, i, optionText
Set optionElements = Browser("YourBrowser").Page("YourPage").WebElement("tagname:=ul").ChildObjects("tagname:=li")

For i = 0 To optionElements.Count - 1
    optionText = optionElements(i).GetROProperty("innertext")
    MsgBox "Option " & (i + 1) & ": " & optionText
Next

内容的提问来源于stack exchange,提问作者Mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:25:35