date_input = driver.find_element_by_xpath("//input[@name='DateBeginDate']") driver.execute_script("arguments[0].removeAttribute('disabled');", date_input) driver.execute_script("arguments[0].removeAttribute('readonly');", date_input) date_input.clear() # 清空输入框内容 date_input.send_keys("2022-12-31") # 输入你想要的日期值 根據提供的html代碼,用這個代碼可以寫入,但是寫上后會立即清空
时间: 2024-04-17 09:23:25 浏览: 12
根据你提供的HTML代码和使用的代码片段,问题可能在于日期输入框的`onchange`事件。该事件可能会在输入日期后立即触发,导致输入的日期被清空。
为了解决这个问题,你可以尝试暂时禁用`onchange`事件,以免它在输入日期后立即触发。以下是一个修改后的代码示例:
```python
date_input = driver.find_element_by_xpath("//input[@name='DateBeginDate']")
driver.execute_script("arguments[0].removeAttribute('readonly');", date_input)
driver.execute_script("arguments[0].removeAttribute('disabled');", date_input)
driver.execute_script("arguments[0].onchange = null;", date_input) # 禁用 onchange 事件
date_input.clear() # 清空输入框内容
date_input.send_keys("2022-12-31") # 输入你想要的日期值
```
通过添加`driver.execute_script("arguments[0].onchange = null;", date_input)`这一行,我们禁用了日期输入框的`onchange`事件,这样在输入日期后就不会立即触发。
尝试运行修改后的代码,并观察是否仍然会在输入日期后立即清空。如果问题仍然存在,请确保没有其他事件或脚本会在输入日期后清空该输入框的值。如果有其他操作,请提供更多相关的代码和细节,以便更好地帮助你解决问题。