Пример #1
0
    def eval_script(self, frame_xpaths, script):
        '''在指定frame中执行JavaScript,并返回执行结果(该实现需要处理js基础库未注入情况的处理)

        :param frame_xpaths: frame元素的XPATH路径,如果是顶层页面,怎传入“[]”
        :type frame_xpaths:  list
        :param script:       要执行的JavaScript语句
        :type script:        string
        '''
        from qt4w.util import JavaScriptError, ControlNotFoundError, ControlAmbiguousError
        try:
            return self._webview.eval_script(frame_xpaths, script)
        except JavaScriptError as e:
            err_msg = e.message
            err_msg = err_msg.split('\n')[0]  # 错误堆栈信息可能会有影响
            err_msg = general_encode(err_msg)
            if ('ReferenceError' in err_msg or 'TypeError'
                    in err_msg) and 'qt4w_driver_lib' in err_msg:
                # ie8 TypeError ie9 ReferenceError
                # 注入js基础库
                self.eval_script(e.frame, self.driver_script)
                return self.eval_script(frame_xpaths, script)
            elif 'Find element' in err_msg and 'failed' in err_msg:
                # 未找到元素
                raise ControlNotFoundError(err_msg)
            elif 'Find' in err_msg and 'elements match' in err_msg:
                err_msg += self._get_dom_tree(frame_xpaths)
                raise ControlAmbiguousError(err_msg)
            else:
                raise e
Пример #2
0
    def _get_dom_tree(self, frame_xpaths):
        '''获取DOM树
        '''
        try:
            from tuia.env import run_env, EnumEnvType
            if run_env != EnumEnvType.Lab:
                return ''
        except ImportError:
            pass

        result = '\nCurrent DOM Tree:\n'
        dom_tree = self.eval_script(
            frame_xpaths, 'document.documentElement.outerHTML;')
        dom_tree = general_encode(dom_tree)
        result += dom_tree
        return result
Пример #3
0
 def _my_encode(self, text):
     '''对于中文,统一处理成unicode编码
     如“中国”,变成“\u4e2d\u56fd”
     '''
     text = unicode_decode(text)
     return general_encode(text.encode('raw_unicode_escape'))