简体   繁体   中英

Selenium gives "selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary" on Mac

Trying to get selenium to work with Python 3 for web scraping purposes:

from selenium import webdriver
chrome_path = r"/Library/Frameworks/Python.framework/Versions/3.6/bin/chromedriver"
driver = webdriver.Chrome(chrome_path)

I get the following error message:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

A similar question was addressed here , but what is baffling to me is that Chrome is already installed on my system. The other asker apparently didn't have it on their computer. I'm running latest version of Mac OS.

The issue is that chromedriver also needs to know where chrome is. In your case it is at a non-default path. So you need to specify the complete path to the Google Chrome binary.

options = webdriver.ChromeOptions()
options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
chrome_driver_binary = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)

Above code is what you should use

I have met this annoying problem when I am lerning selenium. This is my solution: (MacOS 10.13.4)

  1. uninstall my chrome
  2. use homebrew to install chromedriver: brew cask install chromedriver
  3. use homebrew to install chrome: brew cask install google-chrome

Thanks to homebrew now chrome and chromedriver are installed in the same folder and this problem will be automatically solved.

It is important on Win to set the name of chrome.exe otherwise it fail to create a process (see below):

  from selenium import webdriver
  from webdriver_manager.chrome import ChromeDriverManager

  options = webdriver.ChromeOptions()
  options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
  chrome_driver_binary = r"C:/Users/Max/.wdm/chromedriver/75.0.3770.8/win32/chromedriver.exe"
  driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)
  driver.get('http://web.whatsapp.com')

selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create a Chrome process.

For Firefox (download driver https://github.com/mozilla/geckodriver/releases ):

  options = webdriver.FirefoxOptions()
  #options.add_argument('-headless')
  #options.binary_location = r"C:\maxbook\maxboxpython\geckodriver-v0.24.0-win64\geckodriver.exe"
  options.binary_location = r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
  firefox_driver_binary = r"C:\maxbook\maxboxpython\geckodriver-v0.24.0-win64\\"
  driver = webdriver.Firefox(firefox_driver_binary, options=options)
options = webdriver.ChromeOptions()
options.binary_location = r"<YOUR_CHROME_PATH>\chrome.exe"
chrome_driver_path = r"<PATH_TO_CHROME_DRIVER>\chromedriver.exe>"

browser = webdriver.Chrome(chrome_driver_path, chrome_options=options)

If anyone is getting the same error on a linux machine, then you are missing google chrome installation as one of the steps needed for chrome driver to work.

Follow this link to install Google chrome on Linux.

Now, check code

driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=chrome_options, service_args=['--verbose', '--log-path=/tmp/chromedriver.log'])

For me it worked.

If your chromedriver is located within /Library/Frameworks/Python.framework/Versions/3.6/bin/ directory the following code block should be working for you:

from selenium import webdriver

chrome_path = r'/Library/Frameworks/Python.framework/Versions/3.6/bin/chromedriver'
driver = webdriver.Chrome(executable_path=chrome_path)
driver.get('https://www.google.co.in')

就我而言,我安装了 Chrome 浏览器,然后它不会抛出错误。

This article gives the full breakdown on what is happening. Installing the latest web driver should fix the problem rather than changing the code.

Basically version 85 of chrome was installed in a different location, however this only affected new installations so it wasn't noticeable for most people.

The latest web driver understands about the new location so getting an updated driver is the easiest solution - that is of course unless you specifically need to test an older version.

Location of drivers.

您只需要下载最新版本的 chrome 和 chromedriver 并安装它

I recently solved this issue by simply downloading the Chrome Browser . Download it and to download latest version of chrome driver by using this code

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

For mac: Note space between Chrome and .app

options = webdriver.ChromeOptions()
options.binary_location = "/Applications/Google Chrome .app/Contents/MacOS/Google Chrome"
chrome_driver_binary = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Selenium.common.exceptions.WebDriverException: Message: unknown error: no chrome binary selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Opera binary with OperaDriver Selenium and Python raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary selenium.common.exceptions.WebDriverException: Message: unknown error: cannot kill Chrome error with gallium and Docker selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at localhost:8733 \\\\ What's going on? selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create a Chrome process error with ChromeDriver Chrome Selenium selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start with Selenium and Chrome on RaspberryPi selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create Chrome process with ChromeDriver Chrome with Selenium Python selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM