Firefox Build 不适用于 Selenium

Posted

技术标签:

【中文标题】Firefox Build 不适用于 Selenium【英文标题】:Firefox Build does not work with Selenium 【发布时间】:2015-02-03 23:28:54 【问题描述】:

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

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

一些设置信息:

火狐33 硒 2.44.0 Python 3.4(也试过 2.7,也不行) Firefox 使用 Ubuntu 14.04 构建

【问题讨论】:

这是一个零星的问题还是一致的?我也看到了这一点,但只是有时(FF34、selenium2.44.0、python2.7、ubuntu12.04)。我确实觉得这很奇怪,这发生在你的剧本中间。请注意,除非您指定新配置文件,否则默认配置文件将保存到 /tmp 目录,因此请确保您没有任何脚本或任何可能删除配置文件的东西。 【参考方案1】:

Ubuntu 14.04、火狐 36.0、硒 2.44.0。 同样的问题,通过以下方式解决:

sudo pip install -U selenium

Selenium 2.45.0 支持 FF36。

更新:Selenium 2.53+ 与 FF45 兼容

你可以得到旧的FF版本here

【讨论】:

这应该是下一步:linuxg.net/how-to-install-firefox-36-on-linux-systems 它对我有用,谢谢 经过这么多挫折之后,这是唯一适合我使用 Mac OS 和 Python 3.5 的匹配 非常感谢兄弟,它拯救了我的一天 :)【参考方案2】:

我花了很长时间调试这个并最终放弃了尝试使不兼容的 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

嘘。好吧,好吧,我终于决定存储我想要的特定版本并切换到兼容的 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 代码中做了一些代码修改。所以我不明白为什么稳定版本可以工作,但我的自建版本不行。不过,感谢您的努力!【参考方案3】:

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

sudo pip3 install -U selenium

【讨论】:

【参考方案4】:

对于 El-Capitan,修复了以下问题:

brew install python

然后

sudo pip install --upgrade selenium

【讨论】:

【参考方案5】:

我在使用 FF 36.0 时遇到了同样的问题。

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

【讨论】:

以上是关于Firefox Build 不适用于 Selenium的主要内容,如果未能解决你的问题,请参考以下文章

地理定位不适用于 Firefox

Selenium 2.53 不适用于 Firefox 47

Jquery $.Post 适用于 Firefox 但不适用于 Chrome

CSS 动画过渡不适用于 Firefox

媒体查询不适用于 Firefox

CSS 打字动画不适用于 Firefox