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

Selenium 4.x导入报错:无法从selenium.webdriver.common.by导入by

解决Selenium 4.x中ImportError: cannot import name 'by'的问题

问题出在导入语句的大小写错误:Selenium里用于定位元素的By类是大写开头的,你写的小写by并不存在于selenium.webdriver.common.by模块中,这就是触发导入错误的直接原因。

修正步骤:

  • 将导入语句修改为:
    from selenium.webdriver.common.by import By
    
  • 你代码中后续使用By.NAME的部分是正确的,无需修改。

修正后的完整代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

serv_obj = Service("C:\\Drivers\\chromedriver_win32\\chromedriver.exe")
driver = webdriver.Chrome(service=serv_obj)

driver.get("https://www.testyou.in/Login.aspx")
driver.maximize_window()

driver.find_element(By.NAME, "ctl00$CPHContainer$txtUserLogin").send_keys("abc@gmail.com")

额外提示:路径中的反斜杠需要转义为\\,否则会被解析为转义字符,可能导致后续路径识别错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 08:35:21