无法在Python中将Firefox WebDriver与Selenium连接起来

时间:2014-12-15 17:05:00

标签: http firefox ubuntu selenium selenium-webdriver

我在尝试启动selenium firefox驱动程序时遇到错误。似乎其他人在这一步遇到了障碍,而且网上没有现成的解决方案,所以希望这个问题会有广泛的帮助。似乎firefox在通过selenium的驱动程序启动时无法建立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
log_file中的

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))失败了,并且#34;无法分配请求的地址&#34;错误。我将位置更改为(0.0.0.0,0)并编辑了我的/ etc / hosts中的localhost条目,并且能够以这种方式绑定。不确定是否可能导致当前失败。

VV编辑每个路易斯的请求VV。我指定我更改localhost地址的两行。

def free_port():     &#34;&#34;&#34;     使用套接字确定空闲端口。     &#34;&#34;&#34;     free_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)     free_socket.bind((&#39; 0.0.0.0&#39;,0))#从127.0.0.1更改     free_socket.listen(5)     port = 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)

1 个答案:

答案 0 :(得分:0)

在将我的编辑内容发布到路易斯的评论中时,我看到我的本地主机问题出现在其他位置,因为主机已经硬编码了两次。我有我的服务器主地址问题,将源中的所有内容更改回127,问题解决了。谢谢你提醒我,@ louis,对不起,我的问题并不是很有趣。 SO允许我2天后会接受我自己的回答。