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

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 xvfbsudo pip install pyvirtualdisplay

Once I had done that, this python code worked:

#!/usr/bin/env pythonfrom pyvirtualdisplay import Displayfrom selenium import webdriverdisplay = Display(visible=0, size=(1024, 768))display.start()browser = webdriver.Firefox()browser.get('http://www.ubuntu.com/')print browser.page_sourcebrowser.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 webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesfirefox_capabilities = DesiredCapabilities.FIREFOXfirefox_capabilities['marionette'] = Truefirefox_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 firefoxsudo apt-get upgrade