应为浏览器二进制位置,但无法在默认位置找到二进制,未提供“moz:firefoxOptions.binary”功能

2024-05-16 13:13:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图重新开始使用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就在下载文件夹中

enter image description here


Tags: inpypackageslocalseleniumlineusersappdata
1条回答
网友
1楼 · 发布于 2024-05-16 13:13:34

此错误消息

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安装在自定义位置,则需要通过Options()实例传递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/')
    

参考资料

您可以在以下内容中找到一些相关的详细讨论:

相关问题 更多 >