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
目录
相关文章
|
Web App开发 Windows
Selenium Chrome浏览器版本与chromedriver驱动兼容版本对照表
Selenium Chrome浏览器版本与chromedriver驱动兼容版本对照表
3262 0
Selenium Chrome浏览器版本与chromedriver驱动兼容版本对照表
使用postman测试接口时需要先登录怎么办
使用postman测试接口时需要先登录怎么办
4782 0
使用postman测试接口时需要先登录怎么办
|
存储 缓存 数据可视化
(七)解析Streamlit的数据元素:探索st.dataframe、st.data_editor、st.column_config、st.table、st.metric和st.json的神奇之处(上)
(七)解析Streamlit的数据元素:探索st.dataframe、st.data_editor、st.column_config、st.table、st.metric和st.json的神奇之处
7805 0
|
Python
python打包exe——pyinstaller遇到的那些坑及解决办法
pyinstaller的那些坑 问题一:failed to create process. 问题二:pyinstaller相关参数
5135 0
python打包exe——pyinstaller遇到的那些坑及解决办法
|
Web App开发 数据采集 Java
【Python】已完美解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created
【Python】已完美解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created
1964 0
|
12月前
|
Web App开发 IDE JavaScript
Selenium IDE下载安装保姆级教程(附安装包,非常详细)
Selenium IDE 是一款开源跨浏览器的Web自动化测试工具,支持“录制-回放”操作,无需编程即可实现UI级端到端测试。基于Electron开发,兼容主流浏览器,可导出多种语言脚本,轻松集成到持续集成流程中,适用于回归测试、兼容性验证等场景。
|
数据库 Python
使用 Django 中的 filter 方法进行数据查询
在 Django 中,QuerySet 的 filter() 方法是一个强大的工具,用于从数据库中检索数据并根据指定的条件进行筛选。在本文中,我们将介绍如何使用 filter() 方法来执行各种类型的数据查询操作。
|
IDE 开发工具 Python
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for MicrosoftEdge
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for MicrosoftEdge
1810 3
|
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
2243 6
Vue2时间轴(Timeline)
本文介绍了一个基于 Vue3 的时间轴组件 `Timeline`,允许用户自定义时间轴宽度和描述文本。通过在 `Timeline.vue` 中使用模板、脚本及样式代码,文章展示了如何创建并使用该组件,并提供了示例代码以供参考。
1621 2
Vue2时间轴(Timeline)