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

如何使用Selenium选择非Select标签的列表下拉框选项

解决方案

你遇到的是自定义组件下拉框的典型问题,该下拉由Material UI实现,未使用原生<select>标签,无法使用Selenium的Select类操作,直接按照普通元素点击流程处理即可:

  • 首先定位下拉框的触发元素,点击展开下拉菜单,确保所有li类型的选项元素渲染可见
  • 再根据目标选项的文本内容或data-value属性定位对应元素,点击即可完成选择

代码示例(Python)

以下示例使用显式等待保证操作稳定性,避免元素未加载导致的报错:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
# 替换为实际的下拉框触发元素定位逻辑,点击展开下拉
dropdown_trigger = driver.find_element(By.XPATH, "替换为你的下拉框触发元素XPATH")
dropdown_trigger.click()

# 方式1:按选项文本匹配,例如要选择(public) public.dwg
target_option = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//li[@role="option" and contains(text(), "(public) public.dwg")]'))
)

# 方式2:按data-value属性匹配,已知属性值时推荐使用,稳定性更高
# target_option = WebDriverWait(driver, 10).until(
#     EC.element_to_be_clickable((By.XPATH, '//li[@role="option" and @data-value="611e693ad6018009c8f7c681"]'))
# )

# 常规点击,若遇元素遮挡可替换为JS点击
target_option.click()
# 遮挡时使用的JS点击方法
# driver.execute_script("arguments[0].click();", target_option)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 01:06:00