【问题标题】:Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided预期的浏览器二进制位置,但无法在默认位置找到二进制文件,未提供“moz:firefoxOptions.binary”功能
【发布时间】:2021-05-06 02:00:53
【问题描述】:

我正在尝试重新使用 Python Webdriver。我这里有代码

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads\geckodriver.exe')
driver.get('http://inventwithpython.com')

这会导致:

C:\Users\Cody\Projects>python accounting.py
Traceback (most recent call last):
  File "accounting.py", line 4, in <module>
    driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads\geckodriver.exe')
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\firefox\webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

如果我尝试:

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads')
driver.get('http://inventwithpython.com')

我明白了

C:\Users\Cody\Projects>python accounting.py
Traceback (most recent call last):
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\common\service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2032.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2032.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "accounting.py", line 4, in <module>
    driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads')
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
    self.service.start()
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\common\service.py", line 86, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'Downloads' executable may have wrong permissions.

Geckodriver.exe 就在下载文件夹中。

【问题讨论】:

    标签: python windows selenium-webdriver firefox geckodriver


    【解决方案1】:

    此错误消息...

    selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
    

    ...暗示GeckoDriver 在尝试启动/生成新的浏览上下文Firefox 浏览器 会话时无法找到 可执行文件。


    原因

    此错误的两个主要原因如下:

    • Firefox 未安装在您的系统中。
    • Firefox 没有安装在您系统的默认位置。

    解决方案

    可能的解决方案是:

    • 如果您的系统中没有安装 Firefox,您需要在执行测试之前安装它。

    • 如果 Firefox 安装在自定义位置,则需要通过 @987654327 传递 firefox 二进制文件的绝对路径,如下所示@实例:

      from selenium import webdriver
      
      options = webdriver.FirefoxOptions()
      options.binary_location = r"C:/location/to/Firefox/Binary/firefox.exe"
      driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads\geckodriver.exe', options=options)
      driver.get('http://inventwithpython.com/')
      

    参考文献

    您可以在以下位置找到一些相关的详细讨论:

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 2021-07-18
      • 1970-01-01
      • 2018-03-31
      • 2021-09-22
      • 2021-03-02
      • 1970-01-01
      • 2020-11-04
      • 2022-06-29
      相关资源
      最近更新 更多