InvalidArgumentException:消息:使用GeckoDriver Firefox Selenium和Python,二进制文件不是Firefox可执行文件错误 [英] InvalidArgumentException: Message: binary is not a Firefox executable error using GeckoDriver Firefox Selenium and Python
问题描述
我遇到与FireFox二进制文件有关的硒错误.
I am having trouble with a selenium error related to a FireFox binary.
我使用反斜杠将C:/Users/Mack/AppData/Local/Programs/Python/添加到PATH并重新启动.我在这里下载了我认为正确的文件 https://github.com/mozilla/geckodriver/releases 我将文件放在连接PATH的目录中.
I added C:/Users/Mack/AppData/Local/Programs/Python/ to PATH using backslashes and rebooted. I downloaded what I thought is the correct file here https://github.com/mozilla/geckodriver/releases I put the file in the directory connected to PATH.
要解决此问题:我尝试使用两个反斜杠
To remedy this: I tried using two backslashes
binary = FirefoxBinary("C:\\Users\Mack\AppData\Local\Programs\Python\Python38-32\geckodriver-v0.27.0-win64\geckodriver.exe")
会引发相同的错误
我尝试使用一个反斜杠
binary = FirefoxBinary("C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\geckodriver-v0.27.0-win64\geckodriver.exe")
它抛出:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Traceback (most recent call last):
File "C:\Users\Mack\Desktop\hacker-stories\Trends.py", line 32, in <module>
browser = webdriver.Firefox(**firefox_binary=binary**)
File "C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable
代码Test.py
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary("C:/Users/Mack/AppData/Local/Programs/Python/Python38-32/geckodriver-v0.27.0-win64/geckodriver.exe")
print(binary)
browser = webdriver.Firefox(firefox_binary=binary)
options = Options()
options.set_preference("browser.download.folderList",2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir","/Data")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/vnd.ms-excel")
driver = webdriver.Firefox(firefox_options=options)
非常感谢您理解此错误.
Any help understanding this error is much appreciated.
推荐答案
此错误消息...
selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable
...表示您作为 FirefoxBinary()
的参数传递的 binary 文件不是有效的 firefox 可执行文件.
...implies that the binary file which you have passed as an argument to FirefoxBinary()
isn't a valid firefox executable.
您似乎已经将 geckodriver.exe
的绝对路径作为导致错误的 FirefoxBinary()
的参数.
You seem to have passed the absolute path of the geckodriver.exe
as an argument to FirefoxBinary()
which is causing the error.
您需要传递 firefox.exe
的绝对路径,而不是 geckodriver.exe
.此外, firefox_options
现在已弃用,您必须使用 options
,并且可以使用以下解决方案:
Instead of the geckodriver.exe
you need to pass the absolute path of the firefox.exe
. Moreover, firefox_options
is deprecated now and you have to use options
and you can use the following solution:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
options = Options()
options.binary = FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')
options.set_preference("browser.download.folderList",2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir","/Data")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/vnd.ms-excel")
driver = webdriver.Firefox(executable_path=r'C:/Users/Mack/AppData/Local/Programs/Python/Python38-32/geckodriver-v0.27.0-win64/geckodriver.exe', options=options)
参考文献
您可以在以下位置找到几个相关的详细讨论:
References
You can find a couple of relevant detailed discussion in:
- SessionNotCreatedException:消息:预期的浏览器二进制位置,但无法在默认位置找到二进制文件,没有'moz:firefoxOptions.binary'
- 预期浏览器的二进制位置,但无法在默认位置找到二进制,因此未提供"moz:firefoxOptions.binary"功能
- 预期浏览器的二进制位置,但无法在默认位置找到二进制文件,没有使用GeckoDriver提供的'moz:firefoxOptions.binary'功能
这篇关于InvalidArgumentException:消息:使用GeckoDriver Firefox Selenium和Python,二进制文件不是Firefox可执行文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!