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

Selenium send_keys无法输入分隔符,输入数值丢失逗号/小数点

问题:数字输入框输入带分隔符的数值时,分隔符被忽略,仅显示纯数字

我尝试用以下代码调用send_keys方法:

price = driver.find_element(By.XPATH,'//*[@id="contentproform-ppv_cost"]')
price.clear()
price.send_keys("12,99")

但输入框最终只显示1299,手动粘贴也得到同样结果。输入"12.99"时,同样会变成1299。


补充信息:对应的输入标签

<input type="number" id="contentproform-ppv_cost" class="form-control" name="ContentProForm[ppv_cost]" value="5.55" placeholder="Price for end-users" step="0.1" min="1.5" max="50" data-trigger="Input">

临时解决办法

目前我用的临时处理代码:

price.send_keys("12")
for i in range (9):
    price.send_keys(Keys.ARROW_UP)
price.send_keys("9")

问题原因与解决方法

原因

输入框类型为type="number",这类输入框的原生规则会过滤非数字字符。从默认值5.55看,它预期用.作为小数分隔符,但输入后分隔符被过滤,大概率是页面的JS监听了输入事件,额外做了字符过滤或格式化处理,导致.和,都被直接移除,剩余数字拼接成了1299。

正确解决方法

直接通过JS修改输入框的value属性,绕过前端的输入事件监听:

price = driver.find_element(By.XPATH,'//*[@id="contentproform-ppv_cost"]')
driver.execute_script("arguments[0].value = '12.99';", price)

这种方式直接操作DOM属性,不会触发键盘输入相关的事件,能避免前端逻辑对输入内容的过滤。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 20:01:21