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

Python Selenium虚拟显示运行Firefox驱动遇连接拒绝问题求助

Fixing "Message: connection refused" with Firefox 52.5.1 + geckodriver 0.19.1 + pyvirtualdisplay

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).
  • 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)
  • 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 a finally block 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:21:16