title: Trouble connecting Firefox WebDriver with Selenium in Python
body: <p>i'm running into an error in trying to start up the selenium firefox driver. it seems like others have hit snags at this step, and there is no readily available solution online, so hopefully this question will be broadly helpful. it seems like firefox is failing to establish an http server interface when initiated through selenium's driver. it appears that i can run <code>firefox</code> from the command line with no errors. </p>
  
  <p>i should specify that i am doing this via ssh login to a linux container. i'm running python2.7 on Ubuntu 14.04 LTS (GNU/Linux 3.16.3-elastic x86_64). i have the latest version of selenium (2.44) installed, and i'm using firefox 34.0. i'm using xvfb to spoof a display. </p>
  
  <p>below is my code, the error logs, and some related source code. </p>
  
  <pre><code>from selenium import webdriver
  d = webdriver.Firefox()
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
      self.binary, timeout),
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
      self.binary.launch_browser(self.profile)
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 66, in launch_browser
      self._wait_until_connectable()
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 105, in _wait_until_connectable
      raise WebDriverException("Can't load the profile. Profile "
  selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.
  </code></pre>
  
  <p>that error is raised here, due to a timeout: </p>
  
  <pre><code>def _wait_until_connectable(self):
      """Blocks until the extension is connectable in the firefox."""
      count = 0
      while not utils.is_connectable(self.profile.port):
          if self.process.poll() is not None:
              # Browser has exited
              raise WebDriverException("The browser appears to have exited "
                    "before we could connect. If you specified a log_file in "
                    "the FirefoxBinary constructor, check it for details.")
          if count == 30:
              self.kill()
              raise WebDriverException("Can't load the profile. Profile "
                    "Dir: %s If you specified a log_file in the "
                    "FirefoxBinary constructor, check it for details.")
          count += 1
          time.sleep(1)
      return True
  </code></pre>
  
  <p>in the log_file: </p>
  
  <pre><code>tail -f logs/firefox_binary.log 
  1418661895753   addons.xpi  DEBUG   checkForChanges
  1418661895847   addons.xpi  DEBUG   No changes found
  1418661895853   addons.manager  DEBUG   Registering shutdown blocker for XPIProvider
  1418661895854   addons.manager  DEBUG   Registering shutdown blocker for LightweightThemeManager
  1418661895857   addons.manager  DEBUG   Registering shutdown blocker for OpenH264Provider
  1418661895858   addons.manager  DEBUG   Registering shutdown blocker for PluginProvider
  System JS : ERROR (null):0 - uncaught exception: 2147746065
  JavaScript error: file:///tmp/tmplkLsLs/extensions/fxdriver@googlecode.com/components/driver-component.js, line 11507: NS_ERROR_NOT_AVAILABLE: Component is not available'Component is not available' when calling method: [nsIHttpServer::start]
  *** Blocklist::_preloadBlocklistFile: blocklist is disabled
  1418661908552   addons.manager  DEBUG   Registering shutdown blocker for &lt;unnamed-provider&gt;
  </code></pre>
  
  <p>one more point of information. early on, in the firefox driver initalization, <code>socket.bind(('127.0.0.1',0))</code> was failing with a "can't assign requested address" error. i changed the location to (0.0.0.0,0) and edited the localhost entry in my /etc/hosts, and was able to bind that way. not sure if that could be causing the current failure though. </p>
  
  <p>VV edits per louis's request VV . i specify the two lines where i change the localhost address. </p>
  
  <p>def free_port():
      """
      Determines a free port using sockets.
      """
      free_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      free_socket.bind(('0.0.0.0', 0)) # changed from 127.0.0.1
      free_socket.listen(5)
      port = free_socket.getsockname()[1]
      free_socket.close()
      return port</p>
  
  <pre><code>def is_connectable(port):
      """
      Tries to connect to the server at port to see if it is running.
  
      :Args:
       - port: The port to connect.
      """
      try:
          socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          socket_.settimeout(1)
          socket_.connect(("0.0.0.0", port)) # changed again
          socket_.close()
          return True
      except socket.error:
          return False
  </code></pre>
  
  <p>here's the constructor from webdriver:</p>
  
  <pre><code>def __init__(self, firefox_profile=None, firefox_binary=None, timeout=30,
               capabilities=None, proxy=None):
  
      self.binary = firefox_binary
      self.profile = firefox_profile
  
      if self.profile is None:
          self.profile = FirefoxProfile()
  
      self.profile.native_events_enabled = (
          self.NATIVE_EVENTS_ALLOWED and self.profile.native_events_enabled)
  
      if self.binary is None:
          self.binary = FirefoxBinary()
  
      if capabilities is None:
          capabilities = DesiredCapabilities.FIREFOX
  
      if proxy is not None:
          proxy.add_to_capabilities(capabilities)
  
      RemoteWebDriver.__init__(self,
          command_executor=ExtensionConnection("127.0.0.1", self.profile,
          self.binary, timeout),
          desired_capabilities=capabilities,
          keep_alive=True)
      self._is_remote = False
  </code></pre>
  
  <p>here's the constructor from extension_connector:</p>
  
  <pre><code>def __init__(self, host, firefox_profile, firefox_binary=None, timeout=30):
      self.profile = firefox_profile
      self.binary = firefox_binary
      HOST = host
      if self.binary is None:
          self.binary = FirefoxBinary()
  
      if HOST is None:
          HOST = "127.0.0.1"
  
      PORT = utils.free_port()
      self.profile.port = PORT 
      self.profile.update_preferences()
  
      self.profile.add_extension()
  
      self.binary.launch_browser(self.profile)
      _URL = "http://%s:%d/hub" % (HOST, PORT)
      RemoteConnection.__init__(
          self, _URL, keep_alive=True)
  </code></pre>