如何在Investing.com正确输入日期?Selenium日期输入异常问题
问题描述
使用Selenium的.send_keys()方法向Investing.com的日期输入框(type="date")输入类似"2020-01-02"的字符串时,输入的日期与预期完全不符。尝试将字符串转为date类型(通过datetime模块)、调整日期格式为日-月-年都无法解决问题,代码无报错,但日期输入结果错误。
网站的日期输入字段结构:
<input class="absolute left-0 top-0 h-full w-full opacity-0" type="date" max="2024-05-29" value="2024-04-28">
对应的XPath:
//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[3]/div[2]/div[2]/div[3]/div[1]/div[1]/input
完整代码如下:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager import time from datetime import date opt = webdriver.ChromeOptions() opt.add_argument("--start-maximized") opt.add_argument("--enable-javascript") prefs = {"download.default_directory": "Downloads"} # 改为你需要的目录 opt.add_experimental_option("prefs", prefs) opt.add_experimental_option("excludeSwitches", ["disable-popup-blocking"]) userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36' opt.add_argument(f'user-agent={userAgent}') driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=opt) time.sleep(2) driver.execute_cdp_cmd("Page.removeScriptToEvaluateOnNewDocument", {"identifier":"1"}) driver.get(r'https://www.investing.com/portfolio'); assert 'Portfolio & Watchlist - Investing.com' in driver.title print('网站打开成功') username = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'loginFormUser_email'))) username.send_keys("", '我的邮箱') print('输入用户ID完成') password = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'loginForm_password'))) password.send_keys("", '我的密码') print('输入密码完成') driver.find_element(By.XPATH, '//*[@id="signup"]/a').click() links = [ 'https://www.investing.com/commodities/aluminum-historical-data', 'https://www.investing.com/commodities/copper-historical-data?cid=959211', 'https://www.investing.com/commodities/silver-historical-data', 'https://www.investing.com/equities/steel-dynamics-historical-data', 'https://www.investing.com/commodities/iron-ore-62-cfr-futures-historical-data' ] for link in links: print(link) driver.get(link) driver.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/div[1]').click() driver.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/div[2]/div[1]/div[1]/input').send_keys('02-05-2022') driver.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/div[2]/div[2]').click() driver.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/a/div').click()
解决方案
核心原因
HTML5的type="date"输入控件内部仅接受ISO 8601标准格式(YYYY-MM-DD),无论页面展示的是何种日期格式(比如日-月-年),.send_keys()必须传入该格式的字符串才能正确识别。
修复步骤
使用标准格式字符串
将代码中的send_keys('02-05-2022')改为send_keys('2022-05-02'),确保是年-月-日顺序:# 替换原日期输入代码 date_input = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/div[2]/div[1]/div[1]/input')) ) date_input.send_keys('2022-05-02')备选方案:JavaScript直接设置属性
如果send_keys仍有问题(比如元素隐藏导致输入失效),直接通过JS修改输入框的value属性,稳定性更高:date_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, '//*[@id="__next"]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/div[2]/div[1]/div[1]/input')) ) driver.execute_script("arguments[0].value = '2022-05-02';", date_input)额外优化建议
- 移除所有
time.sleep(),改用WebDriverWait显式等待,避免页面加载不完整导致元素定位失败 - 登录代码中
send_keys("", '我的邮箱')可简化为send_keys('我的邮箱'),无需传入空字符串 - 确认日期输入框的定位XPath正确,避免定位到上层装饰元素而非实际接收输入的
type="date"控件
- 移除所有
内容的提问来源于stack exchange,提问作者Andre Gomes
相关产品推荐
相关产品推荐

