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

如何使用Selenium Python为含onkeydown="return false"的日期输入框赋值

问题:操作禁用键盘输入的日期选择框日历控件

我写了一段极简HTML代码,实现带日期限制的选择框,但其中onkeydown="return false"会阻止键盘输入:

<!DOCTYPE html>
<html>
    <body>
        <input type="date" max="2023-03-09" value="2023-03-09" onkeydown="return false">
    </body>
</html>

现在我需要通过浏览器生成的日历控件修改日期,但没法直接访问这个控件,连日历图标都很难定位。目前只能靠固定偏移点击打开日历,肯定有更靠谱的方法。

我的Python测试代码如下:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time


driver = webdriver.Firefox()
driver.get("E:\Web\TestDate\public_html\index.html")
buttonDate = driver.find_element(By.TAG_NAME, "input")
action = ActionChains(driver)
w, h = buttonDate.size['width'], buttonDate.size['height']
x, y = buttonDate.location['x'], buttonDate.location['y']
wx, wy = driver.get_window_size()['width'], driver.get_window_size()['height']
action.move_to_element_with_offset(buttonDate, w - 10, h - 7)
action.click()
action.perform()
time.sleep(30)
driver.quit()

这段代码能打开日历控件,但没法用send_keys()修改日期。


编辑:感谢所有回答,帮了我大忙。我已经采纳了最短、通用性最强的方案,所有方案其实都很不错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 12:05:00