火狐浏览器版本与Selenium不兼容。

30

为了我的研究,我对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")

火狐浏览器可以正常打开并响应(我可以在搜索栏中输入网站)。 但是过一段时间,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

这是偶发问题还是一贯存在的问题?我也遇到过这个问题,但只有时候会出现(FF34,selenium2.44.0,python2.7,ubuntu12.04)。我觉得很奇怪,因为这种情况发生在你的脚本中间。请注意,默认配置文件保存在/tmp目录中,除非您指定一个新的配置文件,因此请确保您没有任何可能删除配置文件的脚本或其他内容。 - Justin
5个回答

38

Ubuntu 14.04,firefox 36.0,selenium 2.44.0。 同样的问题,通过以下方式解决:

sudo pip install -U selenium

Selenium 2.45.0适用于FF36。

更新:Selenium 2.53+与FF45兼容。

您可以在这里获取旧的FF版本。


这应该是下一步:http://linuxg.net/how-to-install-firefox-36-on-linux-systems/,它对我有用,谢谢。 - nono
1
经过这么多的挫折,这是我在Mac OS和Python 3.5上唯一成功的匹配。 - thecheech
非常感谢你,兄弟。这救了我的一天 :) - Fatemeh Rostami

11
我花了很长时间来调试,但最终放弃了尝试让不兼容的selenium/firefox版本工作。我对firefox的专业知识已经无法再深入了。我的建议是从https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/下载稳定版本,并尝试不同的firefox/selenium组合以使其适用于您的环境。对于我来说,这是:

firefox==32.0.3 selenium==2.43.0

我参考了changelog: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

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

感谢您的时间。在调试这个问题时,我和您遇到了同样的问题。奇怪的是,稳定版本的Firefox运行得非常好。不幸的是,我不能使用稳定版本,因为我对Firefox代码进行了一些修改。所以我不明白为什么稳定版本可以工作,但我的自制版本却不能。尽管如此,还是非常感谢您的努力! - Thomas Müller

4

我花了一个小时来解决这个问题。对于Python3,请记得使用pip3,否则它只会在Python2上升级selenium,你会想知道为什么它仍然不起作用。

sudo pip3 install -U selenium


2

对于El-Capitan,需要进行以下修复:

brew install python

然后执行:

sudo pip install --upgrade selenium


-2

我曾经遇到过FF 36.0的同样问题。

我建议你使用命令'pip install -U selenium'将selenium包更新到最新版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接