出现这种问题,请您优化一下代码 > & D:/Python/python.exe c:/Users/Administrator/Desktop/py/test1.py Traceback (most recent call last): File "D:\Python\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 67, in _binary_paths output = SeleniumManager().binary_paths(self._to_args()) File "D:\Python\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 54, in binary_paths return self._run(args) ~~~~~~~~~^^^^^^ File "D:\Python\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 128, in _run raise WebDriverException( f"Unsuccessful command executed: {command}; code: {completed_proc.returncode}\n{result}\n{stderr}" ) selenium.common.exceptions.WebDriverException: Message: Unsuccessful command executed: D:\Python\Lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe --browser chrome --language-binding python --output json; code: 65 {'code': 65, 'message': 'error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)', 'driver_path': '', 'browser_path': ''} The above exception was the direct cause of the following exception: Traceback (most recent call last): File "c:\Users\Administrator\Desktop\py\test1.py", line 13, in <module> driver = webdriver.Chrome() File "D:\Python\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 47, in __init__ super().__init__( ~~~~~~~~~~~~~~~~^ browser_name=DesiredCapabilities.CHROME["browserName"], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<3 lines>... keep_alive=keep_alive, ^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "D:\Python\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 53, in __init__ if finder.get_browser_path(): ~~~~~~~~~~~~~~~~~~~~~~~^^ File "D:\Python\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 47, in get_browser_path return self._binary_paths()["browser_path"] ~~~~~~~~~~~~~~~~~~^^ File "D:\Python\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 78, in _binary_paths raise NoSuchDriverException(msg) from err selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for chrome; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
时间: 2025-07-26 21:21:32 浏览: 22
<think>我们遇到了一个关于Selenium WebDriver无法找到ChromeDriver的问题。错误信息表明Selenium Manager无法自动下载或定位到正确的ChromeDriver版本。
解决方案通常包括:
1. 确保已安装与浏览器版本匹配的ChromeDriver,并将其路径添加到系统PATH中。
2. 或者,在代码中指定ChromeDriver的路径。
由于错误信息指出Selenium Manager尝试从已知版本列表下载时失败(可能是网络问题或URL变更),我们可以手动指定ChromeDriver路径。
修改代码:在初始化webdriver.Chrome时,通过`service`参数指定ChromeDriver的路径。
例如,假设我们将chromedriver.exe放在D:\drivers目录下,可以这样写:
```python
from selenium.webdriver.chrome.service import Service
# 指定ChromeDriver路径
driver_path = r'D:\drivers\chromedriver.exe'
service = Service(executable_path=driver_path)
driver = webdriver.Chrome(service=service)
```
另外,确保Chrome浏览器已安装,并且chromedriver版本与浏览器版本匹配。
如果问题仍然存在,请检查网络连接,或者尝试手动下载chromedriver并指定路径。
下面是修改后的代码:</think>您的错误表明Selenium无法自动找到合适的ChromeDriver版本。以下是优化后的代码解决方案:
```python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os
# 配置参数(需根据实际修改)
SWITCH_URL = "http://192.168.1.1" # 交换机管理地址
PASSWORD = "your_password_here" # 登录密码
PASSWORD_FIELD_ID = "password" # 密码输入框元素ID
LOGIN_BUTTON_XPATH = "//button[@type='submit']" # 登录按钮XPath
CHROME_DRIVER_PATH = r"D:\drivers\chromedriver.exe" # 手动指定ChromeDriver路径
# 初始化浏览器(手动指定ChromeDriver路径)
service = Service(executable_path=CHROME_DRIVER_PATH)
options = webdriver.ChromeOptions()
options.add_argument("--ignore-certificate-errors") # 忽略证书错误
options.add_argument("--start-maximized") # 最大化窗口
driver = webdriver.Chrome(service=service, options=options)
driver.get(SWITCH_URL)
try:
# 显式等待密码框加载
password_field = WebDriverWait(driver, 15).until(
EC.element_to_be_clickable((By.ID, PASSWORD_FIELD_ID))
)
# 输入密码并提交
password_field.send_keys(PASSWORD)
driver.find_element(By.XPATH, LOGIN_BUTTON_XPATH).click()
# 验证登录成功(增加多种验证方式)
WebDriverWait(driver, 15).until(
EC.any_of(
EC.title_contains("Dashboard"),
EC.presence_of_element_located((By.ID, "main-content"))
)
)
print(f"登录成功!当前页面标题: {driver.title}")
except Exception as e:
print(f"登录失败: {str(e)}")
# 截图保存错误信息
driver.save_screenshot("login_error.png")
print("错误截图已保存为login_error.png")
finally:
# 保持浏览器打开以便调试
input("按Enter键关闭浏览器...")
driver.quit()
```
### 关键优化点:
1. **指定ChromeDriver路径**:
- 手动下载匹配Chrome浏览器版本的[ChromeDriver](https://chromedriver.chromium.org/downloads)
- 通过`Service`类明确指定路径
2. **证书错误处理**:
- 添加`--ignore-certificate-errors`选项自动跳过证书警告
3. **增强等待机制**:
- 增加超时时间至15秒
- 使用`element_to_be_clickable`替代`presence_of_element_located`
- 添加`any_of`多条件验证登录成功
4. **错误处理优化**:
- 自动保存错误截图
- 添加交互式关闭(调试时保持浏览器打开)
5. **浏览器配置**:
- 添加`--start-maximized`确保窗口最大化显示
### 操作步骤:
1. 从[ChromeDriver官网](https://chromedriver.chromium.org/downloads)下载匹配您Chrome版本的驱动
2. 解压后将`chromedriver.exe`放在`D:\drivers\`目录(或修改代码中的路径)
3. 替换代码中的`SWITCH_URL`、`PASSWORD`等实际参数
4. 运行前检查浏览器版本与驱动版本是否匹配
阅读全文
相关推荐




















