如何解决Python Selenium报错:‘未找到Chrome二进制文件’
问题分析与解决方案
核心概念纠正
options.binary_location 仅用于指定Chrome浏览器程序的路径,绝非ChromeDriver的路径。ChromeDriver的路径由Service类自动处理(你用ChromeDriverManager()已经完成了这一步),无需手动设置。
错误根源
从Traceback中的/mnt/c/路径可以判断,你大概率是在WSL(Windows子系统Linux)环境下运行代码,直接使用Windows格式的路径(如C:\...)会导致识别失败。
解决方案
方案1:在WSL环境中运行(修正路径格式)
将Windows路径转换为WSL挂载路径,注意空格转义:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager options = webdriver.ChromeOptions() # WSL下的Chrome浏览器挂载路径 options.binary_location = "/mnt/c/Program Files/Google/Chrome/Application/chrome.exe" driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
方案2:切换到Windows原生Python环境运行
直接使用Windows原生路径(推荐用原始字符串避免转义问题):
from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager options = webdriver.ChromeOptions() # Windows原生路径,原始字符串格式 options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe" driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
额外检查步骤
- 确认Chrome实际安装路径:
打开Chrome→右上角三点→设置→关于Chrome,查看浏览器的真实安装路径,确保与代码中填写的一致(部分用户的Chrome可能安装在C:\Users\你的用户名\AppData\Local\Google\Chrome\Application\chrome.exe)。 - Firefox同类问题处理:
Firefox的binary_location同样需指向Firefox浏览器程序路径,而非geckodriver,按上述逻辑调整路径即可。
内容的提问来源于stack exchange,提问作者Tom Price
相关产品推荐
相关产品推荐

