为何创建typing.py后出现selenium.webdriver无Chrome属性错误?
Selenium报错:
AttributeError: partially initialized module 'selenium.webdriver' has no attribute 'Chrome' 原因分析 问题背景
原本运行正常的Selenium代码突然报错,涉及的脚本文件名为typing.py,代码如下:
from selenium import webdriver url = "https://www.livechat.com/typing-speed-test/#/" driver = webdriver.Chrome() driver.get(url=url)
报错详情
driver = webdriver.Chrome() ^^^^^^^^^^^^^^^^ AttributeError: partially initialized module 'selenium.webdriver' has no attribute 'Chrome' (most likely due to a circular import). Did you mean: 'chrome'?
当前项目目录结构
selenium │ .env │ amazon.py │ cookie.py │ linkedin.py │ main.py │ typing.py │ test.py │ └───__pycache__ typing.cpython-312.pyc
报错核心原因
这个错误的根本问题是脚本所在的目录名和第三方Selenium库重名了:你的项目根目录是selenium,而你要导入的官方Selenium库也叫selenium。当Python执行from selenium import webdriver时,会优先加载当前目录下的selenium文件夹(将其视为本地模块),而不是你通过pip安装的官方Selenium库。
这个本地的selenium模块里并没有webdriver.Chrome这个属性,而且Python尝试加载它时,会和真正的Selenium库产生循环导入冲突,最终抛出这个错误。之前代码能正常运行,应该是你之前的项目目录结构里没有这个同名的selenium文件夹,后来调整目录结构后才触发了这个命名冲突。
内容的提问来源于stack exchange,提问作者B Kazimy
相关产品推荐
相关产品推荐

