30

for my research, I did some source code modifications in firefox and build it myself. In order to automate testing, I opted to use Selenium but unfortunately, my newly built Firefox seem to not support Selenium.

I did the following:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary("/path/to/firefox/binary")

d = webdriver.Firefox(firefox_binary=binary)

d.get("http://www.google.de")

The Firefox does open and is responsive (I can enter a website in the search bar). But after a while, the python script crashes with the following error message:

Traceback (most recent call last):
  File "firefox.py", line 7, in <module>
    d = webdriver.Firefox(firefox_binary=binary)
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 66, in launch_browser
    self._wait_until_connectable()
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 109, 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.

I did google that error message and most solutions suggested, that I should Update Selenium since it does not support the Firefox version used. Unfortunately, I installed the newest version of selenium (2.44.0) and I even used an older version of firefox (version 33) to rule out that point.

I also made sure that my code modifications are not the reason for this to crash by building a clean, unmodified firefox. Selenium doesn't work with this firefox either.

If I don't specify a firefox binary and let Selenium use the installed Firefox, everything works fine. So my guess is, that something is wrong with the firefox build, which I did exactly as mentioned in the online documentation (e.g. ./mach build).

Has anyone an idea, what my mistake might be? Any help is greatly appreciated!

Some setup information:

  • Firefox 33
  • Selenium 2.44.0
  • Python 3.4 (also tried 2.7, doesn't work either)
  • Firefox build with Ubuntu 14.04
Thomas Müller
  • 309
  • 1
  • 3
  • 4
  • Is this a sporadic issue or consistent? I'm seeing this as well, but only sometimes (FF34, selenium2.44.0, python2.7, ubuntu12.04). I do find it weird that this is happening to you mid-script. NOTE that the default profile gets saved to the /tmp directory unless you specify a new one, so make sure you don't have any scripts or anything that could be deleting the profile. – Justin Dec 18 '14 at 22:18

5 Answers5

38

Ubuntu 14.04, firefox 36.0, selenium 2.44.0. The same problem, was solved by:

sudo pip install -U selenium

Selenium 2.45.0 is OK with FF36.

update: Selenium 2.53+ is compatible with FF45

You can get older FF versions here

penduDev
  • 4,743
  • 35
  • 37
zesaver
  • 578
  • 4
  • 6
11

I have spent a long time debugging this and ultimately gave up trying to make incompatible versions of selenium/firefox work. I just don't have the expertise in firefox to go any further. My recommendation is downloading stable versions from https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/ and keep trying combinations of firefox/selenium that work for your environment. For me, this is:

firefox==32.0.3 selenium==2.43.0

I'm referring to the changelog here: http://selenium.googlecode.com/git/java/CHANGELOG to see which versions are supposedly compatible.

Basically, what is happening is webdriver is polling on its port until it can establish a socket connection.

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

And then if there is an socket error, keep going. So what you are probably seeing (at least what I am) is the browser hanging for 30 secs.

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(("127.0.0.1", port))
        socket_.close()
        return True
    except socket.error:
        return False

Bleh. Alright, well I finally just decided to store the specific version I want and switch to the compatible selenium version.

bin_dir = os.path.join(const.WEBDRIVER_DIR, 'firefox', 'binary', '32.0.3', 'linux-x86_64', 'firefox')
binary = FirefoxBinary(firefox_path=bin_dir)
driver = webdriver.Firefox(firefox_binary=binary)

I also highly recommend adding a log file to the firefox binary and checking that. Your issue might be unique and any bizarre errors will be logged there:

log_dir = os.path.join(const.LOGS_DIR, 'firefox')
    try:
    os.makedirs(directory)
except OSError, e:
    if e.errno == errno.EEXIST and os.path.isdir(directory):
        pass
log_path = os.path.join(log_dir, '{}.log'.format(datetime.datetime.now().isoformat('_'))
log_file = open(log_path, 'w')
binary = FirefoxBinary(firefox_path=bin_dir, log_file=log_file)
Justin
  • 680
  • 1
  • 8
  • 16
  • Thank you for you time. When debugging this problem, I came to the same point as you. Strange enough, that stable versions of firefox work like a charm. Unfortunately, i can't use stable versions, because I did some code modifications in the firefox code. So i don't get why a stable version is working, but my self-built version not. Nevertheless, thx for your effort! – Thomas Müller Dec 20 '14 at 10:28
4

Spent an hour on this same issue. For Python3 remember to pip3, otherwise it will only upgrade selenium on Python2, and you'll be left wondering why it's still not working.

sudo pip3 install -U selenium

MisterRios
  • 315
  • 3
  • 7
2

For El-Capitan, the followings fixed:

brew install python

then

sudo pip install --upgrade selenium

Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
-2

I suffered the same problem with FF 36.0.

I recommend you update selenium package to latest version with cmd 'pip install -U selenium'.

xtremely
  • 15
  • 1