【问题标题】:selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH with GeckoDriver Firefox Selenium and Pythonselenium.common.exceptions.WebDriverException:消息:“firefox”可执行文件需要在 GeckoDriver Firefox Selenium 和 Python 的 PATH 中
【发布时间】:2019-06-13 03:47:45
【问题描述】:

我正在尝试用 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"),我把驱动放在同一个目录下。

【问题讨论】:

    标签: python python-2.7 selenium firefox geckodriver


    【解决方案1】:

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

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

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

    但是,在使用 Selenium 3.x 时,您需要从 mozilla/geckodriver 下载最新的 GeckoDriver 并将其保存在系统中的任何位置,并提供 GeckoDriver 的绝对路径 em> 通过参数 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
      

    【讨论】:

    • 很明显他正在使用 unix youroptions.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe' 将不起作用,在问题whereis firefox /usr/bin/firefox 中很明显@
    • @timmy 更新了,让我知道你的想法。
    • @timmy 当答案更新后,它解决了我的问题。
    猜你喜欢
    • 2018-07-11
    • 2021-04-15
    • 2018-03-22
    • 2017-08-03
    • 1970-01-01
    • 2018-09-27
    • 2018-02-14
    • 2020-09-11
    • 2017-03-04
    相关资源
    最近更新 更多