python - 预期的浏览器二进制位置,但无法在默认位置找到二进制文件,未提供“moz:firefoxOptions.binary”功能
问题描述
我正在尝试重新使用 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 就在下载文件夹中。
解决方案
此错误消息...
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,您需要在执行测试之前安装它。
如果Firefox安装在自定义位置,则需要通过实例传递Firefox二进制文件的绝对路径,如下所示:
Options()
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/')
参考
您可以在以下位置找到一些相关的详细讨论:
推荐阅读
- python - 为什么我的位置查找功能出现值错误?
- spring - 事务不使用弹簧回滚
- abstract-syntax-tree - Ada 使用 ASIS 修改源代码(Ada Semantics 接口规范)
- design-patterns - Is it normal to share one table's data with many other tables without directly using foreign key
- yii2 - 登录访问在yii2中不起作用
- java - 传递给 List java 的对象的内存引用
- scala - 从 org.apache.spark.sql.Dataset 转换为 CoordinateMatrix
- rust - 为什么作为参数传递的特征对象的生命周期需要更高等级的特征边界而结构不需要?
- visual-studio - 带有 C++/CX 变量的 Visual Studio 条件断点无法评估
- javascript - 不使用特定链接引用脚本