Skip to main content
 首页 » 编程设计

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

2024年12月31日3352php

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

from selenium import webdriver 
driver=webdriver.Firefox() 

但是我得到了以下错误:

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

Selenium using Python - Geckodriver 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

注意:我希望这不是上述链接的副本,因为我尝试了这些答案但没有解决问题。

我从 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并将其保存在系统中的任何位置,并通过参数 executable_path 提供 GeckoDriver 的绝对路径。

以下代码块可以完美地打开 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