Ubuntu 在selenium中使用Chromedriver时Permission denied,权限错误的解决方法

本文解决了一个常见的Selenium自动化测试中遇到的问题:使用ChromeDriver时出现的权限错误。通过修改chromedriver的权限设置,成功解决了WebDriverException错误,确保了自动化测试脚本能够顺利运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误信息如下:

Traceback (most recent call last):
File “/home/ly/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py”, line 76, in start
stdin=PIPE)
File “/usr/lib/python3.6/subprocess.py”, line 709, in init
restore_signals, start_new_session)
File “/usr/lib/python3.6/subprocess.py”, line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: ‘chromedriver’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/ly/Desktop/robot/day_06/01_try_selenium.py”, line 6, in
driver = webdriver.Chrome()
File “/home/ly/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py”, line 73, in init
self.service.start()
File “/home/ly/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py”, line 88, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Process finished with exit code 1

解决方法:

  • 打开终端
whereis chromedriver  # 找到chromediver

返回:chromedriver: /usr/bin/chromedriver

cd /usr/bin/chromedriver # 进入
sudo chmod +x chromedriver # 给予权限

ok

<think>嗯,用户提到了在使用executable_path='chromedriver.exe'出错了。首先,我需要确定这个错误通常发生在什么情况下。一般来说,当使用Selenium库来控制Chrome浏览器,需要指定ChromeDriver的路径。executable_path参数是用来指定这个路径的,但可能用户遇到了路径错误或者版本不匹配的问题。 接下来,我应该考虑常见的错误原因。第一个可能是ChromeDriver没有正确安装或者路径不对。用户可能把chromedriver.exe放在了错误的目录下,或者没有添加到系统环境变量中。这候,如果直接写'chromedriver.exe',Python可能找不到这个文件,除非它位于当前工作目录或者系统路径中。 第二个可能性是版本不兼容。Chrome浏览器和ChromeDriver的版本必须匹配。如果用户最近更新了Chrome浏览器但没有更新ChromeDriver,就会导致版本不匹配的错误。这候需要检查两者的版本,并下载对应的ChromeDriver版本。 然后,用户可能使用Selenium版本较新,而代码的写法已经过了。例如,新版本的Selenium不再需要显式指定executable_path,而是通过Service对象来配置。所以用户可能在代码中使用了类似webdriver.Chrome(executable_path='chromedriver.exe')的旧方法,而新版本应该使用webdriver.Chrome(service=Service('chromedriver.exe'))。 另外,用户可能没有正确导入必要的模块,比如没有从selenium.webdriver.chrome.service导入Service。这候即使路径正确,也会因为缺少导入而报错。 还需要考虑操作系统的问题。例如,在Windows系统中,可能需要指定chromedriver.exe的完整路径,比如C:/path/to/chromedriver.exe,而不仅仅是相对路径。或者,用户可能没有给chromedriver.exe赋予可执行权限,尤其是在Linux或Mac系统下。 此外,杀毒软件或防火墙有会阻止ChromeDriver的运行,导致程序无法启动浏览器。这种情况下,用户需要检查安全软件设置,确保没有阻止ChromeDriver的执行。 总结起来,用户的问题可能涉及路径错误、版本不兼容、过的代码写法、缺少模块导入、权限问题或被安全软件阻止。我应该一步步引导用户检查这些可能的原因,并提供对应的解决方法。比如,建议用户检查文件路径是否正确,确认Chrome和ChromeDriver版本匹配,更新代码以适应新版本的Selenium,确保正确导入Service类,检查文件权限,以及排除安全软件的干扰。</think>在使用 `executable_path='chromedriver.exe'` 出现错误,通常与 **Selenium** 控制浏览器驱动相关(如 ChromeDriver)。以下是常见原因及解决方法: --- ### **1. 路径问题** - **错误现象**:`WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH` - **原因**: `chromedriver.exe` 未放置在正确路径,或代码未正确指定路径。 - **解决方法**: - **方式一**:将 `chromedriver.exe` 放在当前 Python 脚本的目录下。 - **方式二**:指定完整绝对路径: ```python from selenium import webdriver driver = webdriver.Chrome(executable_path=r'C:\your\path\to\chromedriver.exe') ``` --- ### **2. 版本不兼容** - **错误现象**:`SessionNotCreatedException: This version of ChromeDriver only supports Chrome version XX` - **原因**: Chrome 浏览器版本与 ChromeDriver 版本不匹配。 - **解决方法**: 1. 查看 Chrome 版本:浏览器地址栏输入 `chrome://version/`。 2. 下载对应版本的 ChromeDriver:[官方下载地址](https://sites.google.com/chromium.org/driver/)。 3. 替换旧版本驱动文件。 --- ### **3. Selenium 版本更新(关键改动)** - **错误现象**:`TypeError: __init__() got an unexpected keyword argument 'executable_path'` - **原因**: 新版 Selenium(≥4.0)弃用了 `executable_path` 参数,需改用 `Service` 对象。 - **解决方法**: ```python from selenium import webdriver from selenium.webdriver.chrome.service import Service service = Service(executable_path='chromedriver.exe') # 指定驱动路径 driver = webdriver.Chrome(service=service) # 通过 service 参数传递 ``` --- ### **4. 权限或系统问题** - **错误现象**:`Permission denied` 或驱动文件无法执行。 - **解决方法**: - **Windows**:右键 `chromedriver.exe` → 属性 → 取消勾选“阻止此文件”。 - **Linux/Mac**:赋予执行权限: ```bash chmod +x chromedriver ``` --- ### **5. 其他可能原因** - **杀毒软件拦截**:临关闭杀毒软件测试。 - **浏览器未关闭**:确保所有 Chrome 实例已终止。 - **驱动文件损坏**:重新下载驱动文件。 --- ### **完整示例代码(适配新版 Selenium)** ```python from selenium import webdriver from selenium.webdriver.chrome.service import Service # 指定驱动路径 service = Service(executable_path='chromedriver.exe') # 启动浏览器 driver = webdriver.Chrome(service=service) # 打开网页 driver.get("https://www.baidu.com") # 关闭浏览器 driver.quit() ``` --- ### **总结步骤** 1. 确认 `chromedriver.exe` 路径正确。 2. 检查 Chrome 与 ChromeDriver 版本是否一致。 3. 更新代码适配新版 Selenium 的 `Service` 用法。 4. 检查系统权限或杀毒软件拦截问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值