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

使用Python Selenium实现网站登录时无法填入用户名如何解决

问题根因与修复方案

你代码的核心问题如下,按优先级修复即可:

  • 大小写匹配错误:你WebDriverWait等待的元素ID是小写的username,但后续查找元素用的是大写开头的Username,HTML元素ID是大小写敏感的,需要确认目标网站登录页用户名输入框的真实ID,保持等待和查找的ID完全一致。
  • 弃用方法适配:Selenium 4.0+版本已经废弃了find_element_by_id、直接传路径初始化webdriver.Chrome的写法,旧写法会出现定位失败、初始化报错的问题,需要调整为新API写法。
  • 输入前激活输入框:部分网站的输入框需要先点击激活才能正常写入内容,可在输入前增加点击操作,同时可以把等待条件替换为EC.element_to_be_clickable,确保元素可交互后再操作。
  • URL格式错误:你填写的网址用了反斜杠\,HTTP协议标准用正斜杠/,修正为https://www.wyzant.com避免页面加载异常。

修复后的参考代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

path = r'C:\Users\test\Documents\Python Scripts\chromedriver.exe'
service = Service(executable_path=path)
driver = webdriver.Chrome(service=service)

driver.get("https://www.wyzant.com")
link = driver.find_element(By.LINK_TEXT, "LOG IN")
link.click()

# 保持等待的ID和后续查找的ID完全一致,替换为页面真实ID即可
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "Username")))
username = driver.find_element(By.ID, "Username")
username.click()
username.send_keys("test")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 16:24:02