Selenium报错:module 'selenium.webdriver'无'Chrome'属性,求解决方案
解决AttributeError: module 'selenium.webdriver' has no attribute 'Chrome'
这个错误我之前调试Selenium脚本时也碰到过,结合你的代码来看,主要有几个常见原因和对应的解决办法:
1. 修正ChromeDriver的路径与文件名
你的代码里写的路径是"C:\\Python\\Python36\\Lib\\site-packages\\selenium\\webdriver\\chrome.chromedriver.exe",这里有个明显的错误:文件名应该是chromedriver.exe,而非chrome.chromedriver.exe,正确路径应该指向chrome子目录下的驱动文件,比如:
"C:\\Python\\Python36\\Lib\\site-packages\\selenium\\webdriver\\chrome\\chromedriver.exe"
2. 适配Selenium 4.x+的新写法
如果你用的是Selenium 4及以上版本,直接通过webdriver.Chrome()传入路径的写法已经被弃用,需要通过Service类来指定驱动路径,修改后的代码如下:
from selenium import webdriver from selenium.webdriver.chrome.service import Service # 替换成你正确的chromedriver路径 service = Service("C:\\Python\\Python36\\Lib\\site-packages\\selenium\\webdriver\\chrome\\chromedriver.exe") driver = webdriver.Chrome(service=service) driver.get('https://www.google.com')
3. 确保ChromeDriver与浏览器版本匹配
ChromeDriver的版本必须和你本地安装的Chrome浏览器版本完全对应,否则也会引发这类异常。你可以按以下步骤操作:
- 打开Chrome浏览器,点击右上角三个点 → 帮助 → 关于Google Chrome,查看浏览器版本
- 下载对应版本的ChromeDriver替换当前的驱动文件
4. 检查是否存在文件命名冲突
如果你的项目目录里有名为selenium.py的文件,会导致Python导入你自己写的文件而非官方Selenium库,这也会触发这个错误。检查一下项目文件,确保没有和库名重名的脚本。
如果以上方法都试过还是不行,可以尝试重新安装Selenium:
pip uninstall selenium pip install selenium
内容的提问来源于stack exchange,提问作者Sharutkarsh




