How to fix Selenium WebDriverException: The browser appears to have exited before we could connect?

for Googlers, this answer didn't work for me, and I had to use this answer instead. I am using AWS Ubuntu.

Basically, I needed to install Xvfb and then pyvirtualdisplay:

sudo apt-get install xvfb
sudo pip install pyvirtualdisplay

Once I had done that, this python code worked:

#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')
print browser.page_source

browser.close()
display.stop()

Thanks to @That1Guy for the first answer


I was running into this on an (headless) Ubuntu 14.04 server with Jenkins and xvfb installed. I had installed the latest stable Firefox (47) which started a build failing that ran a python script which used the Firefox driver for selenium (version 2.53).

Apparently Firefox 47+ is not compatible with the driver used in Selenium 2.53, and Selenium 3+ will be using a new driver called "Marionette" or "Gecko Driver" (which isn't officially released yet).

This page explains how to use the new driver pretty well, in several languages: https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver

Basically:

  1. get/build the executable from the project on github: https://github.com/mozilla/geckodriver/releases (and make sure it's perms are set to be executable, IE chmod a+x /path/to/geckdriver-executable)
  2. rename/copy binary to "wires"
  3. make sure the binary's location is added to the PATH that the build uses when executing the selenium test
  4. update the selenium test to use the new driver

For Python, step 4 looked something like the following for me:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'

driver = webdriver.Firefox(capabilities=firefox_capabilities)

I too had faced same problem. I was on Firefox 47 and Selenium 2.53; I downgraded Firefox to 45. This worked.

  1. Remove Firefox 47 first :

    sudo apt-get purge firefox
    
  2. Check for available versions:

    apt-cache show firefox | grep Version
    

    It will show available firefox versions like:

    Version: 47.0+build3-0ubuntu0.16.04.1
    Version: 45.0.2+build1-0ubuntu1

  3. Install a specific version

    sudo apt-get install firefox=45.0.2+build1-0ubuntu1
    
  4. Next you have to not upgrade to the newer version again.

    sudo apt-mark hold firefox
    
  5. If you want to upgrade later

    sudo apt-mark unhold firefox
    sudo apt-get upgrade
    

Check your DISPLAY environment variable. Run echo $DISPLAY in the command line.

If nothing is printed, then you are running FireFox without any DISPLAY assigned. You should assign one! Run export DISPLAY=:1 in the command line before running your python script.

Check this thread for more information: http://hashcat.net/forum/thread-1973.html