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

PythonSeleniumSelenium WebdriverWebdriver

Python Problem Overview


I have installed firefox and Xvfb on my centos6.4 server to use selenium webdriver.

But, when I run the code, I got an error.

from selenium import webdriver
browser = webdriver.Firefox()

Error

selenium.common.exceptions.WebDriverException: Message: 
'The browser appears to have exited before we could connect. The output was: None'

I read some related pages on stackoverflow and someone suggested to remove all files in tmp folder, so I did it. But, it still doesn't work.

Could anyone please give me a help?

Thank you in advance!

Edit

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 64, in launch_browser
    self._wait_until_connectable()
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 103, in _wait_until_connectable
    self._get_firefox_output())
selenium.common.exceptions.WebDriverException: Message: 'The browser appears to have exited     before we could connect. The output was: None' 

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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)

Solution 3 - Python

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
    

Solution 4 - Python

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

Solution 5 - Python

I think the simplest solution here is just run Python with xvfb-run:

sudo apt-get install xvfb
xvfb-run python <your_file_or_args>

Solution 6 - Python

Rollback your Firefox to the previous working version. I suggest 2 versions back. Disable Firefox Maintenance Service.

I was working on a solution and the Firefox Maintenance Service updated Firefox to the latest build in the background. This broke my code and it was giving me this error.

Now it is fixed!

Thank you everyone!

Solution 7 - Python

This error is due to your Xvfb is not running. So restart your xvfb:

Xvfb :99 -ac

then check. This works for me.

Solution 8 - Python

Instead of downgrading firefox from 47 version to 45 or something I'll suggest to upgrade to 47.0.1 or above since they seem to fix an issue.

But if your OS doesn't have new packages in repo (for example Ubuntu 14.04 in time of this answer), you can use debs from ubuntuzilla project:

wget sourceforge.net/projects/ubuntuzilla/files/mozilla/apt/pool/main/f/firefox-mozilla-build/firefox-mozilla-build_47.0.1-0ubuntu1_amd64.deb

sudo dpkg -i firefox-mozilla-build_47.0.1-0ubuntu1_amd64.deb

For x86 use _i386.deb postfix. That sold problem for me

Solution 9 - Python

I fixed this by running a recursive chown against not only the python script using selenium, but against the entire virtualenv that script was running in. I changed the ownership to the user running the file. After that, this error went away.

Solution 10 - Python

I also faced the same issue, what I did was:

  1. Upgrade selenium package

     sudo pip install -U selenium
    
  2. Instead of rolling back to older version(like suggested) I rolled up to newer version(48.0, I was previously using V47.0). (for upgrading follow the instructions given by Toby Speight but instead of choosing older version choose newer version)

Solution 11 - Python

I found this solution on Windows 10 Build 18363. I had to call out specifically the Firefoxbinary and the geckdriver executable path.

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX.copy()
caps['marionette'] = True

# Path to Firefox binary
binary = FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')

# Browser (driver) binary assigned, capabilities, and executable path for the geckodriver
driver = webdriver.Firefox(firefox_binary=binary, capabilities=caps,
                           executable_path=r'C:\Users\<name>\python\python-projects\geckodriver-v0.28.0-win64\geckodriver.exe')
# get google.co.in
driver.get("https://google.com")

Solution 12 - Python

update your selenuim version ---> pip install -U selenium

Solution 13 - Python

It can be solved by changing the file permission of the output file ( or related files to the program).
I used Firefox's webdriver.

Try:

chmod -R 777 output_file

This solved me the same trouble you have.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionW3QView Question on Stackoverflow
Solution 1 - PythonDavidjbView Answer on Stackoverflow
Solution 2 - PythonJoel KleierView Answer on Stackoverflow
Solution 3 - PythonAmogh JoshiView Answer on Stackoverflow
Solution 4 - PythonBilal NaqviView Answer on Stackoverflow
Solution 5 - PythonPhilip TzouView Answer on Stackoverflow
Solution 6 - PythonJacob GeorgeView Answer on Stackoverflow
Solution 7 - PythonAshok DevatwalView Answer on Stackoverflow
Solution 8 - PythonvalignatevView Answer on Stackoverflow
Solution 9 - Pythonmelchoir55View Answer on Stackoverflow
Solution 10 - PythonNeeraj KomuravalliView Answer on Stackoverflow
Solution 11 - PythonNexxsysView Answer on Stackoverflow
Solution 12 - Pythonayoub laazizView Answer on Stackoverflow
Solution 13 - PythonZodEnIXView Answer on Stackoverflow