【问题标题】:Trouble connecting Firefox WebDriver with Selenium in Python在 Python 中将 Firefox WebDriver 与 Selenium 连接时出现问题
【发布时间】:2014-12-15 17:05:00
【问题描述】:

我在尝试启动 selenium firefox 驱动程序时遇到了错误。似乎其他人在此步骤中遇到了障碍,并且在线没有现成的解决方案,因此希望这个问题会有所帮助。通过 selenium 的驱动程序启动时,firefox 似乎无法建立 http 服务器接口。看来我可以从命令行运行firefox 而没有错误。

我应该指定我是通过 ssh 登录到 linux 容器来执行此操作的。我在 Ubuntu 14.04 LTS(GNU/Linux 3.16.3-elastic x86_64)上运行 python2.7。我安装了最新版本的 selenium (2.44),我使用的是 firefox 34.0。我正在使用 xvfb 来欺骗显示器。

下面是我的代码、错误日志和一些相关的源代码。

from selenium import webdriver
d = webdriver.Firefox()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 66, in launch_browser
    self._wait_until_connectable()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 105, 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.

由于超时,此处出现该错误:

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

在日志文件中:

tail -f logs/firefox_binary.log 
1418661895753   addons.xpi  DEBUG   checkForChanges
1418661895847   addons.xpi  DEBUG   No changes found
1418661895853   addons.manager  DEBUG   Registering shutdown blocker for XPIProvider
1418661895854   addons.manager  DEBUG   Registering shutdown blocker for LightweightThemeManager
1418661895857   addons.manager  DEBUG   Registering shutdown blocker for OpenH264Provider
1418661895858   addons.manager  DEBUG   Registering shutdown blocker for PluginProvider
System JS : ERROR (null):0 - uncaught exception: 2147746065
JavaScript error: file:///tmp/tmplkLsLs/extensions/fxdriver@googlecode.com/components/driver-component.js, line 11507: NS_ERROR_NOT_AVAILABLE: Component is not available'Component is not available' when calling method: [nsIHttpServer::start]
*** Blocklist::_preloadBlocklistFile: blocklist is disabled
1418661908552   addons.manager  DEBUG   Registering shutdown blocker for <unnamed-provider>

还有一点信息。早期,在 Firefox 驱动程序初始化中,socket.bind(('127.0.0.1',0)) 因“无法分配请求的地址”错误而失败。我将位置更改为 (0.0.0.0,0) 并在我的 /etc/hosts 中编辑了 localhost 条目,并且能够以这种方式绑定。不确定这是否会导致当前的故障。

根据 louis 的要求进行 VV 编辑 VV 。我指定了我更改本地主机地址的两行。

def free_port(): """ 使用套接字确定空闲端口。 """ free_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) free_socket.bind(('0.0.0.0', 0)) # 从 127.0.0.1 改变 free_socket.listen(5) 端口 = free_socket.getsockname()[1] free_socket.close() 返回端口

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(("0.0.0.0", port)) # changed again
        socket_.close()
        return True
    except socket.error:
        return False

这是来自 webdriver 的构造函数:

def __init__(self, firefox_profile=None, firefox_binary=None, timeout=30,
             capabilities=None, proxy=None):

    self.binary = firefox_binary
    self.profile = firefox_profile

    if self.profile is None:
        self.profile = FirefoxProfile()

    self.profile.native_events_enabled = (
        self.NATIVE_EVENTS_ALLOWED and self.profile.native_events_enabled)

    if self.binary is None:
        self.binary = FirefoxBinary()

    if capabilities is None:
        capabilities = DesiredCapabilities.FIREFOX

    if proxy is not None:
        proxy.add_to_capabilities(capabilities)

    RemoteWebDriver.__init__(self,
        command_executor=ExtensionConnection("127.0.0.1", self.profile,
        self.binary, timeout),
        desired_capabilities=capabilities,
        keep_alive=True)
    self._is_remote = False

这是 extension_connector 的构造函数:

def __init__(self, host, firefox_profile, firefox_binary=None, timeout=30):
    self.profile = firefox_profile
    self.binary = firefox_binary
    HOST = host
    if self.binary is None:
        self.binary = FirefoxBinary()

    if HOST is None:
        HOST = "127.0.0.1"

    PORT = utils.free_port()
    self.profile.port = PORT 
    self.profile.update_preferences()

    self.profile.add_extension()

    self.binary.launch_browser(self.profile)
    _URL = "http://%s:%d/hub" % (HOST, PORT)
    RemoteConnection.__init__(
        self, _URL, keep_alive=True)

【问题讨论】:

  • 显示utils.is_connectable的代码并显示启动WebDriver实例的代码。
  • 可能值得查看您正在使用的相应 selenium 绑定的发行说明,以查看支持的 Firefox 版本。

标签: http firefox ubuntu selenium selenium-webdriver


【解决方案1】:

在发布我对 louis 评论的编辑时,我看到我的 localhost 问题出现在其他位置,因为主机被硬编码了两次。我让我的服务器管理员解决了这个问题,将源代码中的所有内容都改回了 127,问题就解决了。谢谢你提示我,@louis,很抱歉我的问题不是更有趣。当 SO 允许我时,我会在 2 天后接受我自己的答案。

【讨论】:

    猜你喜欢
    • 2016-11-15
    • 2013-03-26
    • 1970-01-01
    • 2016-05-03
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    相关资源
    最近更新 更多