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

求助:使用Selenium无法选择Cyberarena网站下拉框选项

解决Selenium无法选中下拉框选项的问题

问题根源

  1. 定位方式不稳定:原代码通过索引[1]定位<select>元素,页面元素结构变化会导致定位错误,应该用更精确的属性定位目标元素。
  2. 等待机制不可靠:固定时长的time.sleep无法保证元素完全加载并可交互,改用显式等待能确保操作时机正确。

修正后的代码(原生select元素处理)

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

if __name__ == '__main__':
    options = Options()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    options.add_argument("start-maximized")
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')  
    srv = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=srv, options=options)    
    link = "https://www.cyberarena.live/schedule-efootball" 
    driver.get(link)  

    # 显式等待目标select元素可见并可交互,最长等待10秒
    wait = WebDriverWait(driver, 10)
    select_element = wait.until(EC.element_to_be_clickable((By.XPATH, "//select[@name='division']")))
    
    select = Select(select_element)
    # 通过可见文本选择选项
    select.select_by_visible_text('Ukrainian Division')
    # 也可通过value值选择(确认value为1时可用)
    # select.select_by_value("1")
    
    input("Press!")  
    driver.quit()

备选方案(自定义下拉组件兼容)

如果目标下拉框是JS模拟的非原生组件,可改用点击展开+选择选项的方式:

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

if __name__ == '__main__':
    options = Options()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    options.add_argument("start-maximized")
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')  
    srv = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=srv, options=options)    
    link = "https://www.cyberarena.live/schedule-efootball" 
    driver.get(link)  

    wait = WebDriverWait(driver, 10)
    # 点击下拉框展开选项列表
    dropdown = wait.until(EC.element_to_be_clickable((By.XPATH, "//select[@name='division']")))
    dropdown.click()
    # 点击目标选项
    target_option = wait.until(EC.element_to_be_clickable((By.XPATH, "//select[@name='division']/option[text()='Ukrainian Division']")))
    target_option.click()
    
    input("Press!")  
    driver.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 16:10:30