From 329e49ea7c908da89c5411887aebfd150e50a065 Mon Sep 17 00:00:00 2001 From: g1879 Date: Sun, 14 Jul 2024 23:01:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=83=E7=B4=A0=E7=AD=89?= =?UTF-8?q?=E5=BE=85=E5=8A=9F=E8=83=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_functions/tools.py | 4 ++-- DrissionPage/_units/waiter.py | 27 ++++++++++++++------------- DrissionPage/_units/waiter.pyi | 1 + 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/DrissionPage/_functions/tools.py b/DrissionPage/_functions/tools.py index cfe4c1e..e9a4be5 100644 --- a/DrissionPage/_functions/tools.py +++ b/DrissionPage/_functions/tools.py @@ -48,9 +48,9 @@ class PortFinder(object): PortFinder.prev_time = perf_counter() if scope in (True, None): scope = (9600, 59600) - msx_times = scope[1] - scope[0] + max_times = scope[1] - scope[0] times = 0 - while times < msx_times: + while times < max_times: times += 1 port = randint(*scope) if port in PortFinder.used_port or port_is_using('127.0.0.1', port): diff --git a/DrissionPage/_units/waiter.py b/DrissionPage/_units/waiter.py index 2483e07..e702946 100644 --- a/DrissionPage/_units/waiter.py +++ b/DrissionPage/_units/waiter.py @@ -390,10 +390,14 @@ class PageWaiter(TabWaiter): class ElementWaiter(OriginWaiter): """等待元素在dom中某种状态,如删除、显示、隐藏""" + def __init__(self, owner): + super().__init__(owner) + self._ele = owner + @property def _timeout(self): """返回超时设置""" - return self._owner.owner.timeout + return self._ele.owner.timeout def deleted(self, timeout=None, raise_err=None): """等待元素从dom删除 @@ -461,7 +465,7 @@ class ElementWaiter(OriginWaiter): timeout = self._timeout end_time = perf_counter() + timeout while perf_counter() < end_time: - if not self._owner.states.is_enabled or not self._owner.states.is_alive: + if not self._ele.states.is_enabled or not self._ele.states.is_alive: return True sleep(.05) @@ -482,8 +486,8 @@ class ElementWaiter(OriginWaiter): end_time = perf_counter() + timeout while perf_counter() < end_time: try: - size = self._owner.states.has_rect - location = self._owner.rect.location + size = self._ele.states.has_rect + location = self._ele.rect.location break except NoRectError: pass @@ -493,10 +497,10 @@ class ElementWaiter(OriginWaiter): while perf_counter() < end_time: sleep(gap) - if self._owner.rect.size == size and self._owner.rect.location == location: + if self._ele.rect.size == size and self._ele.rect.location == location: return True - size = self._owner.rect.size - location = self._owner.rect.location + size = self._ele.rect.size + location = self._ele.rect.location if raise_err is True or Settings.raise_when_wait_failed is True: raise WaitTimeoutError(f'等待元素停止运动失败(等待{timeout}秒)。') @@ -534,7 +538,7 @@ class ElementWaiter(OriginWaiter): :param err_text: 抛出错误时显示的信息 :return: 是否等待成功 """ - a = self._owner.states.__getattribute__(attr) + a = self._ele.states.__getattribute__(attr) if (a and mode) or (not a and not mode): return True if isinstance(a, bool) else a @@ -542,7 +546,7 @@ class ElementWaiter(OriginWaiter): timeout = self._timeout end_time = perf_counter() + timeout while perf_counter() < end_time: - a = self._owner.states.__getattribute__(attr) + a = self._ele.states.__getattribute__(attr) if (a and mode) or (not a and not mode): return True if isinstance(a, bool) else a sleep(.05) @@ -556,11 +560,8 @@ class ElementWaiter(OriginWaiter): class FrameWaiter(BaseWaiter, ElementWaiter): def __init__(self, owner): - """ - :param owner: ChromiumFrame对象 - """ super().__init__(owner) - super(BaseWaiter, self).__init__(owner.frame_ele) + self._ele = owner.frame_ele @property def _timeout(self): diff --git a/DrissionPage/_units/waiter.pyi b/DrissionPage/_units/waiter.pyi index d8824ac..8eb4849 100644 --- a/DrissionPage/_units/waiter.pyi +++ b/DrissionPage/_units/waiter.pyi @@ -109,6 +109,7 @@ class PageWaiter(TabWaiter): class ElementWaiter(OriginWaiter): _owner: ChromiumElement = ... + _ele: ChromiumElement = ... def __init__(self, owner: ChromiumElement): ...