首页 > 解决方案 > Selenium.common.exceptions.WebDriverException:消息:未知错误:没有 chrome 二进制文件

问题描述

我正在尝试运行几个月前制作的 python 脚本,它使用 selenium 来抓取网页。这是我的代码:

import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path="/users/aliallam/Documents/chromedriver")

这是我得到的完整错误:

Traceback (most recent call last):
  File "/Users/aliallam/Desktop/MISOS_Python_Scraper/main.py", line 16, in <module>
    driver = webdriver.Chrome(executable_path="/users/aliallam/Documents/chromedriver")
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

我在这个问题下尝试了解决方案,但仍然没有运气:Selenium 在 Mac 上给出“selenium.common.exceptions.WebDriverException:消息:未知错误:找不到 Chrome 二进制文件”

这是我将代码更改为:

options = webdriver.ChromeOptions()
options.binary_location = " /Applications/Google\ Chrome\ 2.app"
chrome_driver_binary = "/users/aliallam/Documents/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary,chrome_options=options)

这真是令人沮丧,一些帮助将不胜感激!提前致谢!

标签: pythonseleniumselenium-webdriver

解决方案


编辑:除了以下错误,我认为您还需要将二进制位置更改为/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome

根据您发布的内容,我认为该错误是因为此行中第一个引号后有一个额外的空格:

options.binary_location = " /Applications/Google\ Chrome\ 2.app"

尝试将其更改为:

options.binary_location = "/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome"

并重新运行代码。

基于您提供的完整代码:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.binary_location = "/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome"
chrome_driver_binary = "/users/aliallam/Documents/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary,chrome_options=options)

如果这不起作用,您也可以尝试

from selenium import webdriver

options = webdriver.ChromeOptions()
options.binary_location = "/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome"
executable_path = "/users/aliallam/Documents/chromedriver"
driver = webdriver.Chrome(executable_path=executable_path, chrome_options=options)

推荐阅读