Python Selenium 在 Mac 上给出“selenium.common.exceptions.WebDriverException:消息:未知错误:找不到 Chrome 二进制文件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46026987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium gives "selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary" on Mac
提问by Alex Heebs
Trying to get selenium
to work with Python 3 for web scraping purposes:
尝试selenium
使用 Python 3 进行网页抓取:
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
selenium.common.exceptions.WebDriverException:消息:未知错误:找不到 Chrome 二进制文件
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.
这里解决了一个类似的问题,但令我困惑的是 Chrome 已经安装在我的系统上。另一个提问者显然没有在他们的电脑上。我正在运行最新版本的 Mac OS。
回答by Tarun Lalwani
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.
问题是 chromedriver 还需要知道 chrome 在哪里。在您的情况下,它位于非默认路径。所以你需要指定Google Chrome
二进制文件的完整路径。
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
上面的代码是你应该使用的
回答by EckoTan
I have met this annoying problem when I am lerning selenium. This is my solution: (MacOS 10.13.4)
我在学习硒时遇到了这个烦人的问题。这是我的解决方案:(MacOS 10.13.4)
- uninstall my chrome
- use homebrew to install chromedriver:
brew cask install chromedriver
- use homebrew to install chrome:
brew cask install google-chrome
- 卸载我的 chrome
- 使用自制软件安装 chromedriver:
brew cask install chromedriver
- 使用自制软件安装 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.
多亏了 homebrew 现在 chrome 和 chromedriver 安装在同一个文件夹中,这个问题会自动解决。
回答by AshishPatil
If anyone is getting the same error on a linux machine, then you are missing google chromeinstallation as one of the steps needed for chrome driver to work.
如果有人在 linux 机器上遇到相同的错误,那么您缺少google chrome安装作为 chrome 驱动程序工作所需的步骤之一。
Follow this linkto install Google chrome on Linux.
按照此链接在 Linux 上安装 Google chrome。
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.
对我来说它奏效了。
回答by DebanjanB
If your chromedriver
is located within /Library/Frameworks/Python.framework/Versions/3.6/bin/
directory the following code block should be working for you:
如果您chromedriver
位于/Library/Frameworks/Python.framework/Versions/3.6/bin/
目录中,则以下代码块应该适合您:
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')
回答by adrian filipescu
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)
回答by Max Kleiner
It is important on Win to set the name of chrome.exe otherwise it fail to create a process (see below):
在 Win 上设置 chrome.exe 的名称很重要,否则无法创建进程(见下文):
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.
selenium.common.exceptions.WebDriverException:消息:未知错误:无法创建 Chrome 进程。
For Firefox (download driver https://github.com/mozilla/geckodriver/releases):
对于 Firefox(下载驱动程序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)