首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >预期浏览器二进制位置,但无法在默认位置找到二进制文件,因此没有提供“moz:firefoxOptions.binary”功能

预期浏览器二进制位置,但无法在默认位置找到二进制文件,因此没有提供“moz:firefoxOptions.binary”功能
EN

Stack Overflow用户
提问于 2021-02-01 21:29:06
回答 1查看 2.5K关注 0票数 1

我正试图重新开始使用Python。我这里有密码

代码语言:javascript
运行
复制
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads\geckodriver.exe')
driver.get('http://inventwithpython.com')

这导致:

代码语言:javascript
运行
复制
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

如果我试着:

代码语言:javascript
运行
复制
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads')
driver.get('http://inventwithpython.com')

我得到了

代码语言:javascript
运行
复制
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就坐在“下载”文件夹中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-01 21:46:50

这个错误信息..。

代码语言:javascript
运行
复制
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

...implies表示,GeckoDriver在尝试启动/生成新的浏览上下文(即火狐浏览器会话)时,无法找到火狐可执行文件。

原因

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

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

解决方案

可能的解决办法是:

  • 如果您的系统中没有安装Firefox,则需要在执行测试之前安装它。
  • 如果火狐安装在自定义位置,您需要通过Options()实例传递火狐二进制文件的绝对路径,如下所示: 从selenium导入webdriver = webdriver.FirefoxOptions() options.binary_location =r“C:/location/to/Firefox/二进制/Firefox.exe”驱动程序=webdriver.FirefoxOptions options=options) driver.get('http://inventwithpython.com/')

参考文献

您可以在以下几个方面找到相关的详细讨论:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66000401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档