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

使用Selenium无法向密码字段输入数据的问题排查

问题分析与解决

错误原因

你在main函数里出现了变量名冲突的问题:

  • 函数参数password是用户输入的密码字符串
  • 但后续执行password = driver.find_element(By.NAME, "password1")时,把password变量重新赋值成了WebElement对象
  • 调用password.send_keys(password)时,实际是把WebElement对象传给了send_keys方法,而该方法需要接收可迭代的字符串/字符序列,因此抛出TypeError: 'WebElement' object is not iterable错误

修复代码

将密码输入框的变量名改为与参数不同的名称,比如添加_input后缀,修改以下代码段:

原错误代码:

password = driver.find_element(By.NAME, "password1")
password.send_keys(password)

re_password = driver.find_element(By.NAME, "password2")
re_password.send_keys(password)

修复后代码:

password_input = driver.find_element(By.NAME, "password1")
password_input.send_keys(password)

re_password_input = driver.find_element(By.NAME, "password2")
re_password_input.send_keys(password)

额外优化建议

  • 养成变量名见名知意的习惯,避免与函数参数、内置对象重名
  • 元素变量可统一添加_input后缀,明确区分输入框元素与输入内容
  • 注意代码中email元素查找用了By.name(小写),建议统一使用By.NAME(大写),避免潜在语法问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 20:37:32