def read_console_log(self, timeout=None): '''读取一条console.log输出的日志 :param timeout: 读取日志的超时时间,为None表示不会超时 :type timeout: int ''' time0 = time.time() while timeout == None or time.time() - time0 < timeout: result = self._webdriver.read_console_log(self._locator) if result: return result time.sleep(0.1) else: raise TimeoutError('Read console log timeout')
def wait_for_text(self, text, timeout=10, interval=0.5): '''暂停程序执行,直到当前元素的InnerText变为特定值 :param text: 要等待的特定值 :type text: string :param timeout: 超时时间 :type timeout: int或float :param interval:重试间隔时间 :type interval: int或float ''' time0 = time.time() while time.time() - time0 < timeout: if self.inner_text == text: return time.sleep(interval) raise TimeoutError('等待控件文本超时:期望值:"%s",当前值:"%s"' % (text, self.inner_text))
def wait_for_attribute(self, name, value, timeout=10, interval=0.5): '''暂停程序执行,直到当前元素的指定属性变为特定值 :param name: 要等待的属性名 :type name: string :param value: 要等待的属性值 :type value: string :param timeout:超时时间 :type timeout: int或float :param interval:重试间隔时间 :type interval:int或float ''' time0 = time.time() while time.time() - time0 < timeout: real_value = self.attributes[name] real_value = real_value.replace('"', '') if real_value == value: return time.sleep(interval) raise TimeoutError('等待控件属性%s超时:期望值:"%s",当前值:"%s"' % (name, value, self.inner_text))
def _pre_click(self, x_offset=None, y_offset=None, highlight=True): '''点击前的处理 ''' # 等待控件可见 timeout = 5 time0 = time.time() while time.time() - time0 < timeout: if self.displayed: break time.sleep(0.1) else: raise TimeoutError('控件:%s 在%d秒内不可见' % (self, timeout)) self._webdriver.scroll_to_visible(self._locators) rect = self.rect time0 = time.time() while time.time() - time0 < 2: time.sleep(0.2) # 等待元素位置发生变化 new_rect = self.rect if new_rect != rect: break self._webdriver.scroll_to_visible(self._locators) # 避免某些情况下滑动失败 if highlight: self.highlight() rect = self.rect # 此时坐标可能发生变化,需要重新获取 outer_rect = self._webview.visible_rect # 换算成以WebView左上角为原点的坐标 outer_rect = [0, 0, outer_rect[2], outer_rect[3]] # 计算交集,避免计算出的坐标为窗口外面 left = max(rect[0], outer_rect[0]) top = max(rect[1], outer_rect[1]) right = min(rect[0] + rect[2], outer_rect[0] + outer_rect[2]) bottom = min(rect[1] + rect[3], outer_rect[1] + outer_rect[3]) rect = (left, top, right - left, bottom - top) x_offset = rect[0] + (rect[2] / 2 if not x_offset else x_offset) y_offset = rect[1] + (rect[3] / 2 if not y_offset else y_offset) return x_offset, y_offset