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

使用Selenium ChromeDriver与Chrome时遇AttributeError:模块无Chrome属性求助

问题解决:Selenium启动Chrome报AttributeError

错误原因分析

你的代码存在两个关键问题:

  1. 重复且错误的导入:同时导入from selenium import webdriver和from selenium.webdriver.chrome import webdriver,后者覆盖了前者的webdriver命名空间,导致找不到Chrome类。
  2. 参数使用错误:Selenium 4.x版本已废弃executable_path参数,需通过Service对象传递驱动路径。

修正后的代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# 初始化Service对象
service_obj = Service("D:\\PYTHONWITHSELENIUMTESTING\\Chromedriver\\chromedriver.exe")
# 启动Chrome浏览器
driver = webdriver.Chrome(service=service_obj)
# 打开目标网页
driver.get("https://www.youtube.com/")

额外注意事项

  • 确保Chrome浏览器版本与ChromeDriver版本匹配,版本不兼容也可能引发异常。
  • 可尝试升级Selenium到最新版本:
    pip install --upgrade selenium
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 13:33:32