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

使用Selenium修改指定网站日期输入框值失败,求解决方法

问题分析

直接通过execute_script修改input的value属性无效,是因为该日期组件大概率基于React、Vue等前端框架开发——这类框架的UI渲染依赖内部状态,而非直接的DOM属性。仅修改DOM的value不会同步到组件状态,因此页面显示不会更新。

解决方案

根据组件类型,可选择以下三种方案:

方案1:模拟输入并触发状态更新(通用文本类输入框)

先清除原有内容,输入目标日期后触发change事件,通知组件更新内部状态:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
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('window-size=1920x1080')                               
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')  
    srv = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=srv, options=options)    
    waitWD = WebDriverWait(driver, 10)         
  
    link = "https://www.cyberarena.live/schedule-efootball" 
    driver.get(link)       
    tdayString = "12/03/2022"
    
    # 定位日期输入框
    date_input = waitWD.until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@id,'input_comp')]")))
    # 清除原有内容
    date_input.clear()
    # 输入目标日期
    date_input.send_keys(tdayString)
    # 触发change事件,同步组件状态
    driver.execute_script("arguments[0].dispatchEvent(new Event('change'))", date_input)
    
    input("Press!")

方案2:适配原生date类型输入框

如果该input的type为date,原生组件仅接受yyyy-MM-dd格式的输入,需调整日期字符串格式:

# ... 其余代码与方案1一致
tdayString = "2022-03-12"  # 改为yyyy-MM-dd格式
date_input = waitWD.until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@id,'input_comp')]")))
date_input.clear()
date_input.send_keys(tdayString)

方案3:模拟点击自定义日历组件

若点击输入框会弹出日历面板,需模拟切换年月并点击目标日期(需根据网站实际DOM结构调整定位):

# ... 其余代码与方案1一致
target_day = "12"
target_month = "March"  # 匹配日历面板显示的月份文本
target_year = "2022"

# 点击日期框打开日历
date_input = waitWD.until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@id,'input_comp')]")))
date_input.click()

# 切换到目标年份
current_year = waitWD.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'year-selector')]"))).text
while current_year != target_year:
    waitWD.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class,'prev-year')]"))).click()
    current_year = waitWD.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'year-selector')]"))).text

# 切换到目标月份
current_month = waitWD.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'month-selector')]"))).text
while current_month != target_month:
    waitWD.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class,'prev-month')]"))).click()
    current_month = waitWD.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'month-selector')]"))).text

# 点击目标日期
waitWD.until(EC.element_to_be_clickable((By.XPATH, f"//td[text()='{target_day}']"))).click()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 09:50:33