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

求助:使用pywinauto自动化TradeZero ZeroPro的Strike下拉框选择

问题:自动化TradeZero ZeroPro的Strike下拉框选择失败

我要自动化TradeZero ZeroPro股票/期权交易桌面程序的操作,现在卡在了名为Strike的下拉框选择上。这个下拉框不支持文本输入,只能从列表里选,列表选项很多带滚动条,我需要选中中间的“430.00”选项。

目前遇到的核心问题是pywinauto会频繁自动把焦点切到窗口左上角的文本框,导致各种尝试都失败。

元素属性信息

先格式化目标价格为和列表一致的字符串:

strike_price_str = "{:.2f}".format(strike_price)
print(strike_price_str)

输出:

430.00

获取下拉框的属性:

strike_dropdown_element = option_chain_window_element["StrikeComboBox"]
print("Dropdown list's combo box properties:")
print(strike_dropdown_element.get_properties(), "\n")

输出:

Dropdown list's combo box properties:
{'class_name': 'WindowsForms10.Window.8.app.0.3ddfb6e_r7_ad1', 
'friendly_class_name': 'ComboBox', 
'texts': ['', ''], 
'control_id': 81007534, 
'rectangle': <RECT L22, T462, R107, B484>, 
'is_visible': True, 
'is_enabled': True, 
'control_count': 2, 
'is_keyboard_focusable': True, 
'has_keyboard_focus': False, 
'automation_id': 'comboStrike'}

获取下拉列表的所有选项文本:

texts = strike_dropdown_element.texts()
print(texts)

输出:

['370.00', '380.00', '382.00', '383.00', '384.00', '385.00', '386.00', '387.00', '388.00', '389.00', '390.00', '391.00', '392.00', '393.00', '394.00', '395.00', '396.00', '397.00', '398.00', '399.00', '400.00', '401.00', '402.00', '403.00', '404.00', '405.00', '406.00', '407.00', '408.00', '409.00', '410.00', '411.00', '412.00', '413.00', '414.00', '415.00', '416.00', '417.00', '418.00', '419.00', '420.00', '421.00', '422.00', '423.00', '424.00', '425.00', '426.00', '427.00', '428.00', '429.00', '430.00', '431.00', '432.00', '433.00', '434.00', '435.00', '436.00', '437.00', '438.00', '439.00', '440.00', '441.00', '442.00', '443.00', '444.00', '445.00', '446.00', '447.00', '448.00', '449.00', '450.00', '451.00', '452.00', '453.00', '454.00', '455.00', '456.00', '457.00', '458.00', '459.00', '460.00', '461.00', '462.00', '463.00', '464.00', '465.00', '466.00', '467.00', '468.00', '469.00', '470.00', '471.00', '472.00', '473.00', '474.00', '475.00', '476.00', '477.00', '478.00', '479.00', '480.00', '481.00', '482.00', '483.00', '484.00', '485.00', '486.00', '487.00', '488.00', '489.00', '490.00', '491.00', '492.00', '493.00', '494.00', '495.00', '496.00', '497.00', '498.00', '499.00', '500.00', '505.00', '510.00', '515.00', '520.00']

获取目标选项的属性:

item_to_select = strike_dropdown_element.child_window(title=strike_price_str, control_type="ListItem")
print("Item to select's properties:")
print(item_to_select.get_properties(), "\n")

输出:

Item to select's properties:
{'class_name': '', 
'friendly_class_name': 'ListItem', 
'texts': ['430.00'], 
'control_id': None, 
'rectangle': <RECT L0, T0, R0, B0>, 
'is_visible': True, 
'is_enabled': True, 
'control_count': 0, 
'is_keyboard_focusable': False, 
'has_keyboard_focus': False, 
'automation_id': '[Editor] [valuelist] ValueListItem 50'}

已尝试的方案及失败结果

  • 方向键滚动:循环按向下键,每次先给下拉框设焦点,但焦点会自动跳回左上角文本框,导致每2-3秒才滚动一次,效率极低,不可行。

    texts = strike_dropdown_element.texts()
    while True:
        strike_dropdown_element.set_focus()
        if strike_dropdown_element.texts()[0] == strike_price_str:
            break
        else:
            strike_dropdown_element.type_keys("{DOWN}", pause=0.1)
            continue
    
  • 点击下拉框+点击选项:调用click()报错AttributeError;改用click_input()能展开下拉框,但点击选项时焦点跳走,下拉框自动收起,选项未被选中。

    strike_dropdown_element.click_input()
    item_to_select.click_input()
    
  • 直接调用select方法:无论是否先展开下拉框,均报_ctypes.COMError: (-2146233067, None, (None, None, None, 0, None))错误。
    示例代码:

    strike_dropdown_element.select(strike_price_str)
    # 或
    strike_dropdown_element.click_input()
    strike_dropdown_element.child_window(title=strike_price_str, control_type="ListItem").select()
    
  • 滚动后点击选项:每次展开下拉框后滚动只生效一次,后续循环不再滚动,无法定位目标选项。

    max_scrolls = 50
    scroll_count = 0
    while scroll_count < max_scrolls:
        strike_dropdown_element.click_input()
        x, y = pyautogui.position()
        scroll((x,y), -5 * scroll_count)
        try:
            item_to_select.click()
            break
        except Exception as e:
            scroll_count += 1
    

解决方案

针对这个问题,推荐以下几种可行方案:

方案1:使用SendKeys直接输入文本定位

很多WinForms下拉框允许通过快捷键展开后输入文本快速匹配选项,即使输入框不可见:

from pywinauto.keyboard import send_keys

# 强制给下拉框设焦点,用Alt+Down展开下拉框
strike_dropdown_element.set_focus()
send_keys("%{DOWN}")

# 输入目标价格,系统自动滚动到匹配项
send_keys(strike_price_str)

# 按回车选中
send_keys("{ENTER}")

方案2:利用选项索引直接选择

通过已获取的选项文本列表找到目标索引,直接定位点击:

texts = strike_dropdown_element.texts()
target_index = texts.index(strike_price_str)

# 展开下拉框
strike_dropdown_element.click_input()

# 通过索引选择选项
strike_dropdown_element.items()[target_index].click_input()

如果items()方法无效,改用children()筛选ListItem:

list_items = strike_dropdown_element.children(control_type="ListItem")
for item in list_items:
    if item.texts()[0] == strike_price_str:
        item.click_input()
        break

方案3:锁定窗口焦点后操作

先激活目标窗口并锁定焦点,避免自动跳转:

# 激活窗口并等待就绪
option_chain_window_element.set_focus()
option_chain_window_element.wait('active', timeout=5)

# 执行下拉框操作
strike_dropdown_element.click_input()
send_keys(strike_price_str)
send_keys("{ENTER}")

方案4:pyautogui坐标定位兜底

如果以上方法都失效,直接用坐标和模拟输入:

import pyautogui

# 获取下拉框中心坐标
rect = strike_dropdown_element.rectangle()
dropdown_x = (rect.left + rect.right) // 2
dropdown_y = (rect.top + rect.bottom) // 2

# 点击展开下拉框
pyautogui.click(dropdown_x, dropdown_y)

# 输入目标价格后回车
pyautogui.typewrite(strike_price_str)
pyautogui.press('enter')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 19:44:50