63

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' 
kenorb
  • 155,785
  • 88
  • 678
  • 743
W3Q
  • 909
  • 1
  • 11
  • 19
  • Which selenium and firefox versions do you use? Thanks. – alecxe Sep 27 '14 at 03:36
  • 4
    Thank you for your comment! Mozilla Firefox 31.1.0. – W3Q Sep 27 '14 at 03:49
  • I have installed selenium via pip command. It is "Version: 2.43.0" – W3Q Sep 27 '14 at 03:50
  • One option: you can output `pip freeze` and see what is there for `selenium`. – alecxe Sep 27 '14 at 03:50
  • Also, what if you try chrome? `browser = webdriver.Chrome()`? – alecxe Sep 27 '14 at 03:51
  • Thanks for your help. I found out that selenium's version is 2.43.0. – W3Q Sep 27 '14 at 03:52
  • When I run "webdriver.Chrome()", I got this error. "selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path." I haven't installed "chrome" onto my server. – W3Q Sep 27 '14 at 03:53
  • Hey, any progress with the issue? Did you have any success with chrome? – alecxe Sep 27 '14 at 19:00
  • I have installed Google Chrome, but it's not working.When I run "webdriver.Chrome(executable_path="/usr/bin/google-chrome")", I get this error."selenium.common.exceptions.WebDriverException: Message: 'Can not connect to the ChromeDriver' " – W3Q Sep 28 '14 at 01:30
  • I'm not sure if this is causing this error. when I run $ google-chrome , I get this error "google-chrome: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by google-chrome)" – W3Q Sep 28 '14 at 01:32
  • I've tried updating gcc but I got errors while compiling it. This is beyond my capacity, I guess... I've spent more than at least 10 hours for selenium webdriver but no outcome at all. I feel kinda depressed now :( – W3Q Sep 28 '14 at 02:48
  • There was a known bug which supposedly was fixed: https://code.google.com/p/selenium/issues/detail?id=7823 However, I have the latest version of selenium and firefox but I also get the error you have here. I've double checked permissions on the python file as well as the /tmp directory which it writes logs too. Everything seems fine. I have no idea why it won't work. – melchoir55 Nov 15 '14 at 03:10

13 Answers13

76

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

Community
  • 1
  • 1
Davidjb
  • 1,190
  • 1
  • 12
  • 19
27

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)
Joel Kleier
  • 386
  • 3
  • 3
  • 1
    If it was working for you in the past, and after a Firefox update you're now getting "WebDriverException: Message: The browser appears to have exited before we could connect.", this is probably the explanation. – medmunds Jun 13 '16 at 23:04
  • Here's more info: https://github.com/SeleniumHQ/selenium/issues/2110#issuecomment-224413609. Alternatives to switching to Marionette right now are reverting to FF 46, or waiting for Mozilla to release a FF 47 patch that fixes this. – medmunds Jun 13 '16 at 23:10
  • 1
    At step 3 I put the file at: `/usr/local/bin/wires`. Thanks! – Caumons Jun 23 '16 at 09:56
  • The bug is now fixed in FF 47.0.1 – seb Jul 27 '16 at 08:35
  • 1
    Update: It appears that FF48 again create problems; neither the original webdriver.Firefox version or the version including marionette work (the second giving back "WebDriverException: Service wires unexpectedly exited. Status code was: 1") – Ando Jurai Aug 08 '16 at 07:08
22

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
    
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Amogh Joshi
  • 449
  • 5
  • 13
11

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

burkemw3
  • 450
  • 3
  • 16
Bilal Naqvi
  • 140
  • 1
  • 11
  • Helpful tip: If you are running Ubuntu 14.04 in Headless mode, you likely want `export DISPLAY=:99`. This is what worked for me. – steve-gregory Dec 04 '15 at 14:49
  • 1
    Ran into this with Jenkins Xvfb plugin: its default config sets `DISPLAY=:0`, which is invalid. You need to override in job config > build environment > xvfb advanced: "Xvfb display name offset". Easy to track down with `echo $DISPLAY` in your build script. – medmunds May 24 '16 at 02:08
5

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>
Philip Tzou
  • 5,926
  • 2
  • 18
  • 27
  • Could you give a quick explanation as to what running python via xvfb-run does? The manpage isn't very helpful – YPCrumble Jun 09 '16 at 05:53
  • 1
    @YPCrumble xvfb has nothing to do with the executable running inside. The problem is about the selenium version or the firefox used with it. Best way to solve this kind of problems is to attach a copy of firefox with your script instead of run your script inside a environment that you don't control/know. – m3nda Jun 12 '16 at 16:01
  • @YPCrumble per your question, xvfb-run is same than pipe anything to xvfb, but in a reverse way. – m3nda Jun 12 '16 at 16:02
1

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!

1

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

Xvfb :99 -ac

then check. This works for me.

Nander Speerstra
  • 1,496
  • 6
  • 24
  • 29
Ashok Devatwal
  • 448
  • 4
  • 15
1

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

valignatev
  • 6,020
  • 8
  • 37
  • 61
0

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.

melchoir55
  • 6,842
  • 7
  • 60
  • 106
0

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)

0

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")
Nexxsys
  • 25
  • 3
-2

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

ayoub laaziz
  • 1,196
  • 9
  • 12
-7

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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
ZodEnIX
  • 9
  • 3
  • @ZodEnlX Oh Thank you for your comment!! It is the old post and actually I've given up for this. I should definitely try your solution!! which file's permission should I change? I mean, in your case, which file's permission did you change? – W3Q Oct 16 '14 at 15:58