Firefox Build does not work with Selenium

后端 未结 5 849
迷失自我
迷失自我 2020-12-29 22:01

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

5条回答
  •  醉梦人生
    2020-12-29 22:34

    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)
    

提交回复
热议问题