Selenium2library浏览器版本问题

3 篇文章 0 订阅
2 篇文章 0 订阅

【问题描述】

Robot Framework(下称RF)测试库Selenium2library打开Firefox浏览器(Open Browser)时,

有时候会遇到以下错误:

[ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: No browser is open
Log:     c:\users\x230\appdata\local\temp\RIDEiqowaw.d\log.html
Report:  c:\users\x230\appdata\local\temp\RIDEiqowaw.d\report.html

【问题剖析】

1、网上有个方案:http://wenwen.sogou.com/z/q398269835.htm

修改:[PythonPath]\Lib\site-packages\Selenium2Library\utils\browsercache.py
将    def close(self):
         if self.current:
             browser = self.current
             browser.quit()  ---->quit修改为close
             self.current = self._no_current
             self.current_index = None
             self._closed.add(browser)
这里的意思是,Selenium 2.0 webdriver浏览器关闭函数是close,而不是现在的quit。

这个方案,我试过,可是不成功....可仔细分析,你会发现这个方案就是个忽悠

原因:浏览器都没启动,何来关闭报错

按理,Python是个解析性语言,而且quit函数还放在一个类的IF分支中,

应该不会影响到生成类的对象。

同时,我查看Selenium2library源代码,Selenium的版本是2.32,关闭函数是quit,而不是close。

所以,此方案不通

2、Firefox版本问题

官方给出的解释如下:

Firefox: Versions supported using native events are the current version, immediately previous version, current extended support release (ESR) version and immediately previous ESR release, as determined from the date of release of the Selenium components. The last public Selenium release at the time of this writing was on 27 August 2013, and at the time, that means the supported Firefox versions were 23 (current), 22 (previous), 17 (current ESR), and 10 (previous ESR). This is complicated by the fact that this version support is only applies to so-called "native events"; if you don't use native events, a much wider range of Firefox versions is supported.

可见,selenium 2.0 支持的Firefox版本有23、22、17、10。

所以,我们应该Firefox的版本下降至23,才能解决问题。我实践过,此方案可行.

【引申】 什么是Native Events?

官网的解释是:

In WebDriver advanced user interactions are provided by either simulating the Javascript events directly (i.e. synthetic events) or by letting the browser generate the Javascript events (i.e. native events). Native events simulate the user interactions better whereas synthetic events are platform independent, which can be important in Linux when alternative window managers are used, seenative events on Linux. Native events should be used whenever it is possible.

Native Events:让浏览器产生Javascript事件

Synthetic Events:WebDriver直接模拟Javascript事件

上文【 if you don't use native events, a much wider range of Firefox versions is supported.

如果不适用Native Events,selenium可以支持更多版本的Firefox。

FirefoxDriver开启Native Events的方法如下:

FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true)
FirefoxDriver driver = new FirefoxDriver(profile);
selenium2library开启Native Events的方式如下:

1、找到文件:【PythonPath】\D:\software\python2.7\Lib\site-packages\Selenium2Library\resources\firefoxprofile\prefs.js

2、在文件末尾添加:user_pref("webdriver_enable_native_events", true);

我设置 webdriver_enable_native_events 等于 false,再跑用例,还是启动不了Firefox。

所以,这里的wider range of Firefox 说的是23以下的

3、更新selenium webdriver:下载selenium-2.44.0,将里面的webdriver整个目录,覆盖原来Selenium下的webdriver

对比新旧版本的代码,问题主要出在:utils.is_connectable返回值一直为False

    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 == 3
                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

is_connectable源码如下:

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

旧版本一直连接不上的原因,我没有去深究。

方案3,经实践可行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值