selenium.common.exceptions.WebDriverException:消息:'firefox' 可执行文件需要与 GeckoDriver Firefox Selenium 和 Python 一起位于 PATH 中

selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH with GeckoDriver Firefox Selenium and Python

我正在尝试用 selenium 打开 Firefox,我试过了

from selenium import webdriver
driver=webdriver.Firefox()

但是我得到以下错误:

selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH.

我试过了

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('/usr/bin/firefox')
browser = webdriver.Firefox(firefox_binary=binary)

也试过

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps['marionette'] = True
caps['binary'] = '/usr/bin/firefox'
d = webdriver.Firefox(capabilities=caps)

`但还是不行。

但是,当我尝试使用上面的代码将最后一行替换为

d=webdriver.Firefox(capabilities=caps,executable_path='/usr/bin/firefox') 并且让我的 Firefox 从后台关闭它会打开 Firefox 但我不能简单地 d.get("https://www.google.com") 它卡在 Linux 主页 并且不打开任何东西。

在终端输入 whereis firefox 后,我得到 /usr/bin/firefox,如果重要的话,我使用 python 2.7

注意:我希望这不是上述内容的重复link,因为我尝试了答案但没有解决。

我从github安装了geckodriver,并尝试了browser=webdriver.Firefox(executable_path="geckodriver"),我把驱动程序放在了同一个目录。

仍然不清楚为什么您会看到以下错误:

selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH.

在大多数情况下,与 PATH 相关的常见错误与 geckodriver 相关。

但是,在使用 Selenium 3.x 时,您需要从 mozilla/geckodriver 下载最新的 GeckoDriver 并将其保存在系统中的任何位置并提供绝对GeckoDriver 通过参数 executable_path.

的路径

以下代码块可以完美打开 Firefox Nightly Browser(安装在自定义位置):

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    
    options = Options()
    options.binary_location = '/path/to/firefox'
    driver = webdriver.Firefox(firefox_options=options, executable_path='/path/to/geckodriver')
    driver.get('http://google.com/')
    print("Page title is: %s" %(driver.title))
    driver.quit()
    
  • 控制台输出:

    Page title is: Google