Firefox Build不适用于Selenium

时间:2014-12-05 17:55:53

标签: python firefox selenium build

对于我的研究,我在firefox中做了一些源代码修改并自己构建。为了自动化测试,我选择使用Selenium但不幸的是,我新建的Firefox似乎不支持Selenium。

我做了以下事情:

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")

Firefox确实打开并且响应迅速(我可以在搜索栏中输入网站)。但过了一会儿,python脚本崩溃并出现以下错误消息:

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.

我确实谷歌那个错误消息和大多数解决方案建议,我应该更新Selenium,因为它不支持使用的Firefox版本。不幸的是,我安装了最新版本的selenium(2.44.0),我甚至使用旧版本的firefox(版本33)来排除这一点。

我还确保通过构建一个干净的,未经修改的firefox,我的代码修改不是导致崩溃的原因。 Selenium也无法使用这个firefox。

如果我没有指定firefox二进制文件并让Selenium使用已安装的Firefox,那么一切正常。所以我的猜测是,firefox构建出了问题,我完全按照在线文档中提到的那样(例如./mach build)。

有谁有想法,我的错误可能是什么?非常感谢任何帮助!

一些设置信息:

  • Firefox 33
  • Selenium 2.44.0
  • Python 3.4(也试过2.7,也不起作用)
  • 使用Ubuntu 14.04构建Firefox

5 个答案:

答案 0 :(得分:37)

Ubuntu 14.04,firefox 36.0,selenium 2.44.0。 同样的问题,解决了:

sudo pip install -U selenium

使用FF36,Selenium 2.45.0正常。

更新:Selenium 2.53+与FF45兼容

您可以使用较旧的FF版本here

答案 1 :(得分:11)

我花了很长时间调试这个并最终放弃尝试制作不兼容的selenium / firefox版本。我只是没有在Firefox中的专业知识进一步。我的建议是从https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/下载稳定版本,并继续尝试适用于您的环境的firefox / selenium组合。对我来说,这是:

火狐== 32.0.3 硒== 2.43.0

我在这里指的是更改日志:http://selenium.googlecode.com/git/java/CHANGELOG以查看哪些版本可以兼容。

基本上,正在发生的事情是webdriver在其端口上轮询,直到它可以建立套接字连接。

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

然后如果出现套接字错误,继续。所以你可能看到的(至少我是)是浏览器挂了30秒。

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。好吧,我终于决定存储我想要的特定版本并切换到兼容的selenium版本。

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)

我还强烈建议将日志文件添加到firefox二进制文件并检查它。您的问题可能是唯一的,并且会在那里记录任何奇怪的错误:

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)

答案 2 :(得分:4)

在同一个问题上花了一个小时。对于Python3,请记住pip3,否则它只会升级Python2上的selenium,并且你会想知道为什么它仍然无效。

sudo pip3 install -U selenium

答案 3 :(得分:2)

对于El-Capitan,以下修正:

brew install python

然后

sudo pip install --upgrade selenium

答案 4 :(得分:-2)

我遇到了与FF 36.0相同的问题。

我建议您使用cmd'pip install -U selenium'将selenium软件包更新到最新版本。

相关问题