Python Selenium虚拟显示运行Firefox驱动遇连接拒绝问题求助
Hey there, let's work through this connection refused error together—since you're using older versions of Firefox and geckodriver, there are several targeted fixes to try:
Verify geckodriver setup & compatibility
Geckodriver 0.19.1 is technically compatible with Firefox 52.x, but you need to make sure it's properly configured:- Specify the full path to geckodriver when initializing the driver (this avoids PATH-related issues):
driver = webdriver.Firefox(firefox_profile=ffprofile, executable_path='/path/to/your/geckodriver') - Ensure geckodriver has executable permissions: run
chmod +x /path/to/your/geckodriver(for Linux/macOS systems).
- Specify the full path to geckodriver when initializing the driver (this avoids PATH-related issues):
Tweak FirefoxProfile for older versions
Older Firefox builds need specific preferences to play nice with geckodriver. Add these lines to your profile setup:ffprofile.set_preference("marionette.enabled", True) ffprofile.set_preference("network.dns.disableIPv6", True)The first ensures Marionette (the protocol linking Firefox and geckodriver) is enabled, while the second avoids potential DNS resolution conflicts that can cause connection failures.
Check pyvirtualdisplay dependencies
Pyvirtualdisplay relies on Xvfb to create a virtual display. Make sure Xvfb is installed on your system:- For Ubuntu/Debian:
sudo apt-get install xvfb - For CentOS/RHEL:
sudo yum install xorg-x11-server-Xvfb
You can also try specifying a unique display number when starting the virtual display to avoid conflicts with other processes:display = Display(visible=0, size=(800, 600), display_num=10)
- For Ubuntu/Debian:
Improve resource cleanup in your code
Your original code doesn't guarantee proper cleanup of the driver or virtual display if an error occurs. Update your code to use afinallyblock to ensure resources are released:from selenium import webdriver from selenium.webdriver import FirefoxProfile from pyvirtualdisplay import Display display = None driver = None try: display = Display(visible=0, size=(800, 600)) display.start() ffprofile = FirefoxProfile() ffprofile.set_preference("dom.webnotifications.enabled", False) ffprofile.set_preference("marionette.enabled", True) ffprofile.set_preference("network.dns.disableIPv6", True) # Replace with your geckodriver path driver = webdriver.Firefox(firefox_profile=ffprofile, executable_path='/usr/local/bin/geckodriver') driver.get('https://www.91mobiles.com') print(driver.current_url) except Exception as e: print(f"Error: {str(e)}") finally: if driver: driver.quit() # Use quit() instead of close() to fully terminate the driver if display: display.stop()Test without virtual display first
To rule out pyvirtualdisplay as the cause, temporarily remove the display-related code and run the script directly. If Firefox launches and loads the page successfully, the issue is isolated to the virtual display setup—double-check Xvfb installation and display configuration in that case.
内容的提问来源于stack exchange,提问作者Maccaroni

