You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python2.7+Selenium调用Firefox遇异常,多方案尝试仍未解决

Issue with Selenium + Firefox: Cannot Launch Browser or Navigate to URLs on Python 2.7

I'm trying to get Selenium to work with Firefox using Python 2.7, but I've hit a bunch of roadblocks that I can't work through on my own. Let me walk through what I've tried so far:

First, I started with the most basic code to launch the browser:

from selenium import webdriver
driver = webdriver.Firefox()

Right away, I got this error:

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

I tried two common fixes I found, but neither worked:

  • Specifying FirefoxBinary directly:
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    binary = FirefoxBinary('/usr/bin/firefox')
    browser = webdriver.Firefox(firefox_binary=binary)
    
  • Configuring via DesiredCapabilities:
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    caps = DesiredCapabilities.FIREFOX
    caps['marionette'] = True
    caps['binary'] = '/usr/bin/firefox'
    d = webdriver.Firefox(capabilities=caps)
    

I managed to get Firefox to launch once I modified the DesiredCapabilities code to include executable_path and closed all background Firefox processes:

d = webdriver.Firefox(capabilities=caps, executable_path='/usr/bin/firefox')

But now there's a new problem: when I run d.get("https://www.google.com"), the browser just stays on the Linux home page and won't navigate to the specified URL.

A few more details about my setup:

  • Running whereis firefox in the terminal returns /usr/bin/firefox
  • I downloaded geckodriver from GitHub and placed it in the same folder as my script. I also tried pointing directly to it with:
    browser = webdriver.Firefox(executable_path="geckodriver")
    
    But that didn't fix the original launch issue either.

I'm stuck here—any help figuring out how to get Firefox to both launch properly and navigate to URLs would be much appreciated!

内容的提问来源于stack exchange,提问作者Amjasd Masdhash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:44:56