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

Selenium脚本出现AttributeError及chromedriver路径问题如何解决?

问题解决指南

1. 关于已修复的构造函数问题

你之前遇到的AttributeError: 'Fetch_Info' object has no attribute 'driver',核心原因是把类的初始化方法误写为__int__而非__init__。Python中只有__init__是类的构造初始化方法,__int__是用于整数类型转换的特殊方法,所以之前的代码里driver属性根本没被初始化。修正为__init__后这个问题就彻底解决,和Selenium版本无关,没必要来回切换版本。

2. 解决'chromedriver.exe' executable needs to be in PATH错误

这个错误是系统无法定位ChromeDriver可执行文件导致的,以下三种方法可解决:

  • 方式一:将chromedriver.exe加入系统PATH

    1. 下载与你的Chrome浏览器版本完全匹配的ChromeDriver(例如Chrome 114.x对应ChromeDriver 114.x)
    2. 将chromedriver.exe放到系统已有的PATH目录中(比如Windows的C:\Windows\System32,或Python安装目录下的Scripts文件夹)
    3. 重启命令行/开发工具后重新运行脚本
  • 方式二:在代码中指定ChromeDriver绝对路径
    初始化WebDriver时直接传入executable_path参数:

    from selenium import webdriver
    
    class Fetch_Info:
        def __init__(self):
            # 替换为你的chromedriver.exe实际路径
            self.driver = webdriver.Chrome(executable_path=r"C:\your\path\chromedriver.exe")
    
  • 方式三:使用Selenium Service类(推荐,适配Selenium 4+)
    Selenium 4开始官方推荐用Service类管理驱动,既能解决路径问题,还能消除过时警告:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    
    class Fetch_Info:
        def __init__(self):
            # 替换为你的chromedriver.exe实际路径
            service = Service(r"C:\your\path\chromedriver.exe")
            self.driver = webdriver.Chrome(service=service)
    

3. 消除DeprecationWarning警告

这个警告是因为使用了Selenium 4+标记为过时的executable_path参数,采用上面的方式三使用Service类即可彻底消除该警告,同时兼容新版本Selenium。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 13:03:17