Firefox Build不适用于Selenium

Tho*_*ler 30 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")
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

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

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

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

有谁有想法,我的错误可能是什么?任何帮助是极大的赞赏!

一些设置信息:

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

小智 38

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

sudo pip install -U selenium
Run Code Online (Sandbox Code Playgroud)

使用FF36,Selenium 2.45.0正常.

更新:Selenium 2.53+与FF45兼容

您可以在这里获得较旧的FF版本

  • 更新评论确实有帮助!谢谢 :) (2认同)

Jus*_*tin 11

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

firefox == 32.0.3 selenium == 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
Run Code Online (Sandbox Code Playgroud)

然后,如果存在套接字错误,请继续.所以你可能看到的(至少我是)是浏览器挂了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
Run Code Online (Sandbox Code Playgroud)

的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)
Run Code Online (Sandbox Code Playgroud)

我还强烈建议将日志文件添加到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)
Run Code Online (Sandbox Code Playgroud)