You need to enable JavaScript to run this app. 如何使用Python Selenium点击下拉列表选项操作STITCH下载页
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python Selenium点击下拉列表选项操作STITCH下载页

Python-Selenium 实现STITCH下载页指定交互的脚本方案

要完成的交互流程为:打开指定STITCH下载页,依次触发物种选择浮层、选中Homo sapiens选项、点击Update按钮提交配置。

前置准备

  • 安装所需依赖包:执行pip install selenium webdriver-manager完成安装
  • 本地预先安装可正常使用的Chrome浏览器

脚本实现逻辑

结合提供的前端代码片段,物种选择浮层通过toggleSpeciesFloatingDiv()函数控制显隐,浮层内的物种选择列表DOM id为speciesList,所有操作均基于这些固定定位元素编写,规避依赖不稳定的临时类名导致的定位失败问题。

完整可运行脚本

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# 初始化浏览器,自动匹配对应Chrome版本的驱动
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
# 访问目标页面
target_url = "http://stitch.embl.de//cgi/download.pl?UserId=FCCY8Z7drB9z&sessionId=QFV3kq1R2gdD"
driver.get(target_url)
# 设置最长15秒的元素等待时长,解决页面加载慢导致的定位失败问题
wait = WebDriverWait(driver, 15)

try:
    # 唤起物种选择浮层:直接调用前端已存在的全局JS方法,兼容性优于直接点击触发控件
    driver.execute_script("toggleSpeciesFloatingDiv()")
    # 等待物种选择列表加载完成
    species_select = wait.until(EC.element_to_be_clickable((By.ID, "speciesList")))
    # 遍历下拉选项选中智人
    for option in species_select.find_elements(By.TAG_NAME, "option"):
        if option.text.strip() == "Homo sapiens":
            option.click()
            break
    # 定位Update按钮并点击提交
    update_button = wait.until(
        EC.element_to_be_clickable((By.XPATH, "//*[normalize-space(text())='Update']"))
    )
    update_button.click()
    # 等待页面刷新完成
    time.sleep(3)
    print("全流程操作执行完成")
finally:
    # 调试阶段可注释掉下面这行,保留浏览器窗口查看操作效果
    # driver.quit()

常见问题排查

  • 浮层无法正常唤起时,检查toggleSpeciesFloatingDiv方法是否为页面全局可访问方法,适当延长页面加载等待时间后再执行JS调用即可解决。
  • 选中Homo sapiens的操作失效时,检查speciesList元素的标签类型:如果是自定义下拉列表而非原生select标签,可直接定位浮层内文本为“Homo sapiens”的可点击元素执行点击。
  • Update按钮定位不到或点击失效时,可检查按钮是否被其他浮层遮挡,通过JS执行点击可规避遮挡问题:driver.execute_script("arguments[0].click();", update_button)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.13 05:36:03