【问题标题】:Selenium.common.exceptions.WebDriverException: Message: unknown error: no chrome binarySelenium.common.exceptions.WebDriverException:消息:未知错误:没有 chrome 二进制文件
【发布时间】:2021-07-18 20:31:15
【问题描述】:

我正在尝试运行几个月前制作的 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 gives "selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary" on Mac

以下是我将代码更改为:

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)

这真是令人沮丧,非常感谢一些帮助!提前致谢!

【问题讨论】:

  • 看起来您可能在二进制位置添加了一个额外的空格,请尝试将其取出并重新运行您的代码:options.binary_location = "/Applications/Google\ Chrome\ 2.app"(额外的空格位于第一个引号之后)

标签: python selenium selenium-webdriver


【解决方案1】:

编辑:除了以下错误,我认为您还需要将二进制位置更改为/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)

【讨论】:

  • 谢谢!删除空格不起作用,但您编写的其他代码可以。
猜你喜欢
  • 2019-10-06
  • 2018-02-12
  • 1970-01-01
  • 2020-10-16
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多