树莓派3用Python3+Selenium调用Firefox遇connection refused错误求助
Hey there! That connection refused error is super common when setting up Selenium with Firefox on a Raspberry Pi—let's walk through the fixes step by step.
The Root Cause (Most Likely)
Selenium can't talk to Firefox directly—it needs a bridge called GeckoDriver to handle the communication. Chances are you haven't installed this driver, or it's not configured correctly for your Pi's ARM architecture.
Step 1: Make Sure Firefox is Installed
First, confirm you have Firefox (the ESR version is recommended for Raspberry Pi) installed:
firefox --version
If it's missing, install it with:
sudo apt update && sudo apt install firefox-esr -y
Step 2: Install GeckoDriver
This is the critical piece. You have two easy ways to get it:
Option 1: Install via APT (Simplest)
Raspberry Pi's repo has a pre-built GeckoDriver package. Run:
sudo apt install firefox-geckodriver -y
After installation, test if it works by running:
geckodriver
You should see output like 169... INFO Listening on 127.0.0.1:4444—that means it's ready.
Option 2: Manual Download (If APT Version is Outdated)
If the APT version doesn't match your Firefox version, grab the ARM-compatible GeckoDriver (look for builds matched to your Firefox release). Then:
- Extract the downloaded binary file
- Move it to a directory in your system PATH, like
/usr/local/bin:sudo mv geckodriver /usr/local/bin/ - Make it executable:
sudo chmod +x /usr/local/bin/geckodriver
Step 3: Update Your Test Script
Now, adjust your script to either:
- Use the PATH-configured GeckoDriver (if you installed via APT or moved it to PATH):
from selenium import webdriver from selenium.webdriver.firefox.options import Options # Add headless mode if you're running without a desktop options = Options() options.headless = True # Remove this if you have a physical desktop connected driver = webdriver.Firefox(options=options) # Rest of your script stays the same... - Or specify the GeckoDriver path explicitly:
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver', options=options)
Extra Troubleshooting Tips
- If you're running the Pi headless (no monitor), always use the
headlessoption—Firefox can't launch a GUI without a display. - Check that your user has permission to run GeckoDriver (the
chmod +xstep takes care of this). - If you still get errors, try updating all packages:
sudo apt update && sudo apt upgrade -yto ensure Firefox and GeckoDriver are on compatible versions.
内容的提问来源于stack exchange,提问作者Nick Evans

