鱼C论坛

 找回密码
 立即注册
查看: 1619|回复: 3

[已解决]py3.11.0,初始化WebDriver 错误

[复制链接]
发表于 2024-9-6 10:05:50 | 显示全部楼层 |阅读模式
10鱼币
py3.11.0,初始化WebDriver 错误


  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.common.keys import Keys
  4. from selenium.webdriver.chrome.service import Service
  5. from selenium.webdriver.chrome.options import Options
  6. import time
  7. # 设置ChromeDriver路径
  8. chrome_driver_path = 'C:\chromedriver'  # 请替换为你的ChromeDriver路径

  9. # 配置ChromeOptions
  10. chrome_options = Options()
  11. chrome_options.add_argument("--start-maximized")  # 启动时最大化窗口
  12. chrome_options.add_argument("--disable-infobars")  # 禁用信息栏

  13. # 初始化WebDriver
  14. service = Service(chrome_driver_path)
  15. driver = webdriver.Chrome(service=service, options=chrome_options)

  16. print("test")
复制代码



错误提示:


  1. Traceback (most recent call last):
  2.   File "D:\installmy\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 64, in _binary_paths
  3.     raise ValueError(f"The path is not a valid file: {path}")
  4. ValueError: The path is not a valid file: C:\chromedriver

  5. The above exception was the direct cause of the following exception:

  6. Traceback (most recent call last):
  7.   File "C:\Users\Administrator\Desktop\xmx-test\waigua20240905-2.py", line 17, in <module>
  8.     driver = webdriver.Chrome(service=service, options=chrome_options)
  9.   File "D:\installmy\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
  10.     super().__init__(
  11.   File "D:\installmy\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 50, in __init__
  12.     if finder.get_browser_path():
  13.   File "D:\installmy\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 47, in get_browser_path
  14.     return self._binary_paths()["browser_path"]
  15.   File "D:\installmy\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 78, in _binary_paths
  16.     raise NoSuchDriverException(msg) from err
  17. 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
复制代码
最佳答案
2024-9-6 10:05:51
wideband 发表于 2024-09-06 10:20
谢谢您的回复,增加了测试文件存在语句,但是 的确有 ...


感谢您提供更多的信息。看来问题是因为路径格式引起的。

为了避免Windows路径中的转义字符问题,可以使用以下两个方法之一:

1. 使用双反斜杠 `\\` 替代单反斜杠 `\`。
2. 在路径前面加上 `r`,表示原始字符串。

具体代码修改如下:

### 方法一:双反斜杠

  1. chrome_driver_path = 'C:\\chromedriver.exe'
复制代码


### 方法二:原始字符串

  1. chrome_driver_path = r'C:\chromedriver.exe'
复制代码


请确保ChromeDriver的文件名正确(一般是 `chromedriver.exe`),并在修改后重新运行代码。如果仍然有问题,请确认路径和文件名的正确性。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-9-6 10:20:21 | 显示全部楼层
谢谢您的回复,增加了测试文件存在语句,但是 的确有: C:\chromedriver 文件呀。

  1. if not os.path.exists(chrome_driver_path):
  2.     raise FileNotFoundError(f"ChromeDriver not found at {chrome_driver_path}")

复制代码


提示:

  1. Traceback (most recent call last):
  2.   File "C:\Users\Administrator\Desktop\xmx-test\waigua20240905-2.py", line 14, in <module>
  3.     raise FileNotFoundError(f"ChromeDriver not found at {chrome_driver_path}")
  4. FileNotFoundError: ChromeDriver not found at C:\chromedriver.exe
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-1 02:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表