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

Python运行Selenium脚本无法检测到指定路径下的Chromedriver问题求助

参考截图

问题根因

你自定义的Booking类中虽然定义了driver_path参数存储Chromedriver路径,但调用父类webdriver.Chrome的构造方法时未传入该路径,父类默认只会扫描系统环境变量PATH下的路径查找驱动,无法识别你指定的C盘路径。

解决方案

方案1:修改代码适配手动下载的Chromedriver

适配Selenium 3.x/低版本4.x的booking.py代码

import booking.constants as const
from selenium import webdriver


class Booking(webdriver.Chrome):
    def __init__(self , driver_path = "C:\\chromedriver.exe"):
        self.driver_path = driver_path
        # 将驱动路径传入父类构造方法
        super(Booking, self).__init__(executable_path=self.driver_path)

    
    def land_first_page(self):
        self.get(const.BASE_URL)

适配Selenium 4.6+版本的booking.py代码

高版本Selenium要求驱动路径封装到Service对象中传入:

import booking.constants as const
from selenium import webdriver
from selenium.webdriver.chrome.service import Service


class Booking(webdriver.Chrome):
    def __init__(self , driver_path = "C:\\chromedriver.exe"):
        self.driver_path = driver_path
        service = Service(executable_path=self.driver_path)
        super(Booking, self).__init__(service=service)

    
    def land_first_page(self):
        self.get(const.BASE_URL)

方案2:无需手动下载Chromedriver(推荐)

Selenium 4.6及以上版本内置Selenium Manager工具,会自动匹配本地Chrome浏览器版本、下载对应版本的Chromedriver,不需要手动下载、配置路径,代码可直接简化为:

import booking.constants as const
from selenium import webdriver


class Booking(webdriver.Chrome):
    def __init__(self):
        # 无需指定驱动路径,父类会自动处理驱动匹配逻辑
        super(Booking, self).__init__()

    
    def land_first_page(self):
        self.get(const.BASE_URL)

上述修改完成后,你原本的run.py代码无需调整,直接运行即可。

额外排查点

  • 确认C盘根目录下的文件确实名为chromedriver.exe,不要修改解压后的默认文件名,也不要直接把压缩包改后缀使用
  • 确认你下载的Chromedriver大版本和本地安装的Chrome浏览器大版本完全一致,比如Chrome版本为118.x,Chromedriver版本也必须为118.x开头

内容的提问来源于stack exchange,提问作者Anshul Thakur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 09:15:03