NoSuchDriverException

简介: 【8月更文挑战第10天】

错误信息 NoSuchDriverException: Unable to locate or obtain driver for chrome 指出Selenium无法定位Chrome驱动程序。根据您提供的错误信息和检索到的资源,这里是一些可能的解决方案:

1. 确保Selenium版本是最新的

Selenium 4.6及以上版本会自动下载所需的驱动程序。如果您使用的是最新版本的Selenium,但仍然遇到错误,请开启日志记录并提交bug报告。

2. 使用环境变量 PATH

您可以手动下载驱动程序,然后将其放置在系统环境变量 PATH 中已列出的目录,或者将包含驱动程序的目录添加到 PATH 中。

  • 在Windows上,打开命令提示符并执行:
    echo %PATH%
    
  • 在Linux或macOS上,打开终端并执行:
    echo $PATH
    

如果驱动程序的位置不在列出的目录中,可以将其添加到 PATH。添加后,可以通过检查驱动程序的版本来测试是否正确添加:

chromedriver --version

3. 指定驱动程序的位置

如果不想升级到最新版本的Selenium,不希望Selenium为您下载驱动程序,或者无法配置环境变量,可以在代码中指定驱动程序的位置。

首先下载所需的驱动程序,然后创建适用的 Service 类的实例并设置路径:

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

chromedriver_path = '/path/to/chromedriver'
service = Service(executable_path=chromedriver_path)
chrome_options = Options()

driver = webdriver.Chrome(service=service, options=chrome_options)

4. 使用第三方驱动管理库

如果您使用的Selenium版本较旧(建议升级),或者需要Selenium Manager尚未实现的高级功能,可以尝试使用第三方工具来自动更新驱动程序,例如:

  • WebDriverManager (Java)
  • WebDriver Manager (Python)
  • WebDriver Manager Package (.NET)
  • webdrivers gem (Ruby)

5. 检查驱动程序下载链接

确保从官方链接下载了正确版本的驱动程序:

6. 使用 Selenium Manager

如果您正在使用Selenium 4.6或更新版本,可以利用Selenium Manager自动管理驱动程序。使用以下代码自动下载并设置Chromedriver:

from selenium import webdriver
from selenium.webdriver.chrome.service import ChromeService
from selenium.webdriver.chrome.options import Options
from selenium_manager import ChromeDriverManager

service = ChromeService(executable_path=ChromeDriverManager().install())
chrome_options = Options()

driver = webdriver.Chrome(service=service, options=chrome_options)

Traceback (most recent call last):
  File "D:/st_dev/ugot1213/test/04selenium.py", line 12, in <module>
    driver = webdriver.Chrome(service=service)
  File "C:\Program Files\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 50, in __init__
    keep_alive,
  File "C:\Program Files\Python37\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 51, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
  File "C:\Program Files\Python37\lib\site-packages\selenium\webdriver\common\driver_finder.py", line 44, in get_path
    raise NoSuchDriverException(f"Unable to locate or obtain driver for {options.capabilities['browserName']}")
selenium.common.exceptions.NoSuchDriverException: Message: Unable to locate or obtain driver for chrome; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location


Process finished with exit code 1
目录
相关文章
|
C++ 索引 容器
c++string容器-子串获取讲解
c++string容器-子串获取讲解
1343 0
|
Web App开发 Windows
Selenium Chrome浏览器版本与chromedriver驱动兼容版本对照表
Selenium Chrome浏览器版本与chromedriver驱动兼容版本对照表
3152 0
Selenium Chrome浏览器版本与chromedriver驱动兼容版本对照表
|
Python
python打包exe——pyinstaller遇到的那些坑及解决办法
pyinstaller的那些坑 问题一:failed to create process. 问题二:pyinstaller相关参数
4967 0
python打包exe——pyinstaller遇到的那些坑及解决办法
|
9月前
|
Web App开发 IDE JavaScript
Selenium IDE下载安装保姆级教程(附安装包,非常详细)
Selenium IDE 是一款开源跨浏览器的Web自动化测试工具,支持“录制-回放”操作,无需编程即可实现UI级端到端测试。基于Electron开发,兼容主流浏览器,可导出多种语言脚本,轻松集成到持续集成流程中,适用于回归测试、兼容性验证等场景。
|
Web App开发 数据采集 Java
【Python】已完美解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created
【Python】已完美解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created
1904 0
|
数据库 Python
使用 Django 中的 filter 方法进行数据查询
在 Django 中,QuerySet 的 filter() 方法是一个强大的工具,用于从数据库中检索数据并根据指定的条件进行筛选。在本文中,我们将介绍如何使用 filter() 方法来执行各种类型的数据查询操作。
|
文字识别 网络协议 开发工具
GitHub封锁?推荐5个国产的Git仓库替代平台
近日,GitHub对中国区IP的部分限制引发了广泛关注。未登录用户被拒,已登录用户功能受限,南北网络环境差异更显“内卷”。为应对这一挑战,本文推荐了多个国产Git平台:Gitee(码云)、GitCode(CSDN旗下)、CODING(腾讯系)、CodeUP(阿里云支持)及微信代码管理工具。这些平台功能全面、稳定性强,是开发者迁移项目的理想选择。通过同步代码、配置CI/CD流水线等简单步骤,可确保项目平稳过渡。此次事件提醒我们,掌握核心技能与支持国产平台同样重要!
19814 11
|
Web App开发 测试技术 Python
【Python】已解决:selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrom
【Python】已解决:selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrom
2187 6
|
IDE 开发工具 Python
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for MicrosoftEdge
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for MicrosoftEdge
1758 3
|
数据采集 前端开发 测试技术
Selenium中定位元素的9种方法
在Selenium中,定位页面元素是自动化测试和网页爬虫的基础。常用的9种元素定位方法包括:ID、Name、Class Name、Tag Name、CSS Selector、XPath、Link Text、Partial Link Text,以及XPath和CSS选择器的组合使用。每种方法各有优劣,建议根据页面的具体情况和元素的属性选择最合适的方法,并使用显式等待确保元素可用。
2428 5

热门文章

最新文章