首页 > 解决方案 > selenium.common.exceptions.WebDriverException:消息:无法找到 firefox 二进制文件。您可以通过指定“firefox_binary”的路径来设置它

问题描述

我想知道如何使用硒。抓取动态页面。与安装 Firefox 有什么关系吗?

from selenium import webdriver
driver=webdriver.Firefox()

selenium.common.exceptions.WebDriverException: Message: Failed to find firefox binary. You can set it by specifying the path to 'firefox_binary':

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('/path/to/binary')
driver = webdriver.Firefox(firefox_binary=binary)

标签: python-3.xseleniumfirefoxgeckodriverselenium-firefoxdriver

解决方案


此错误消息...

selenium.common.exceptions.WebDriverException: Message: Failed to find firefox binary. You can set it by specifying the path to 'firefox_binary':

...暗示GeckoDriver无法找到firefox二进制文件。

可能Firefox浏览器安装在您机器上的非常规位置,因此GeckoDriver无法找到它。


解决方案

如果Firefox安装在您机器上的非常规位置,您需要传递 Firefox 二进制文件的绝对位置如下所示:

from selenium import webdriver

binary = '/path/to/firefox'
# Example of using Firefox Developer Edition on Windows OS
# binary = r'C:\Program Files\Firefox Developer Edition\firefox.exe'
# Example of using Firefox Nightly Edition on Windows OS
# binary = r'C:\Program Files\Nightly\firefox.exe'

options = webdriver.FirefoxOptions()
options.binary = binary
browser = webdriver.Firefox(firefox_options=options, executable_path='/path/to/geckodriver')
browser.get('http://google.com/')
browser.quit()

您可以在How to open Firefox Developer Edition through Selenium中找到相关讨论


推荐阅读