diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 538ec7c..315c249 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -12,4 +12,4 @@ from ._pages.chromium_page import ChromiumPage from ._pages.session_page import SessionPage from ._pages.web_page import WebPage -__version__ = '4.1.0.0b22' +__version__ = '4.1.0.0b23' diff --git a/DrissionPage/_base/base.py b/DrissionPage/_base/base.py index 84d976c..5b2a945 100644 --- a/DrissionPage/_base/base.py +++ b/DrissionPage/_base/base.py @@ -89,7 +89,7 @@ class BaseElement(BaseParser): @property def timeout(self): - return self.owner.timeout + return self.owner.timeout if self.owner else 10 # ----------------以下属性或方法由后代实现---------------- @property diff --git a/DrissionPage/_base/base.pyi b/DrissionPage/_base/base.pyi index a3aaf6d..c3b995b 100644 --- a/DrissionPage/_base/base.pyi +++ b/DrissionPage/_base/base.pyi @@ -25,6 +25,7 @@ from .._pages.web_page import WebPage class BaseParser(object): _type: str + timeout: float def __call__(self, locator: Union[Tuple[str, str], str], index: int = 1): ... diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index 3413ab3..4e9c1dc 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -141,7 +141,7 @@ class ChromiumElement(DrissionElement): @property def sr(self): - end_time = perf_counter() + self.owner.timeout + end_time = perf_counter() + self.timeout while perf_counter() < end_time: info = self.owner._run_cdp('DOM.describeNode', backendNodeId=self._backend_id)['node'] if info.get('shadowRoots', None): @@ -238,7 +238,8 @@ class ChromiumElement(DrissionElement): return ChromiumElementsList(self.owner, super().afters(locator, timeout, ele_only=ele_only)) def over(self, timeout=None): - timeout = timeout if timeout is None else self.owner.timeout + if timeout is None: + timeout = self.timeout bid = self.wait.covered(timeout=timeout) if bid: return ChromiumElement(owner=self.owner, backend_id=bid) @@ -261,7 +262,8 @@ class ChromiumElement(DrissionElement): x = int(nx) y = int(ny) loc_data = locator_to_tuple(locator) if locator else None - timeout = timeout if timeout is not None else self.owner.timeout + if timeout is None: + timeout = self.timeout end_time = perf_counter() + timeout try: ele = ChromiumElement(owner=self.owner, @@ -437,7 +439,8 @@ class ChromiumElement(DrissionElement): return self._run_js(f'return window.getComputedStyle(this{pseudo_ele}).getPropertyValue("{style}");') def src(self, timeout=None, base64_to_bytes=True): - timeout = self.owner.timeout if timeout is None else timeout + if timeout is None: + timeout = self.timeout if self.tag == 'img': # 等待图片加载完成 js = ('return this.complete && typeof this.naturalWidth != "undefined" ' '&& this.naturalWidth > 0 && typeof this.naturalHeight != "undefined" ' @@ -524,7 +527,7 @@ class ChromiumElement(DrissionElement): if self.tag == 'img': # 等待图片加载完成 js = ('return this.complete && typeof this.naturalWidth != "undefined" && this.naturalWidth > 0 ' '&& typeof this.naturalHeight != "undefined" && this.naturalHeight > 0') - end_time = perf_counter() + self.owner.timeout + end_time = perf_counter() + self.timeout while not self._run_js(js) and perf_counter() < end_time: sleep(.1) if scroll_to_center: @@ -937,7 +940,8 @@ def find_in_chromium_ele(ele, locator, index=1, timeout=None, relative=True): loc_str = f'{ele.css_path}{loc[1]}' loc = loc[0], loc_str - timeout = timeout if timeout is not None else ele.owner.timeout + if timeout is None: + timeout = ele.timeout # ---------------执行查找----------------- if loc[0] == 'xpath': diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 29813c3..9358b7d 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -425,7 +425,8 @@ class ChromiumBase(BasePage): return self._ele(locator, timeout=timeout, index=None) def s_ele(self, locator=None, index=1, timeout=None): - timeout = self.timeout if timeout is None else timeout + if timeout is None: + timeout = self.timeout return (NoneElement(self, method='s_ele()', args={'locator': locator, 'index': index, 'timeout': timeout}) if locator and not self.wait.eles_loaded(locator, timeout=timeout) else make_session_ele(self, locator, index=index, method='s_ele()')) @@ -682,8 +683,8 @@ class ChromiumBase(BasePage): self._alert.handle_next = accept self._alert.next_text = send return - - timeout = self.timeout if timeout is None else timeout + if timeout is None: + timeout = self.timeout timeout = .1 if timeout <= 0 else timeout end_time = perf_counter() + timeout while not self._alert.activated and perf_counter() < end_time: @@ -728,7 +729,8 @@ class ChromiumBase(BasePage): self._has_alert = False def _wait_loaded(self, timeout=None): - timeout = timeout if timeout is not None else self.timeouts.page_load + if timeout is None: + timeout = self.timeouts.page_load end_time = perf_counter() + timeout while perf_counter() < end_time: if self._ready_state == 'complete': diff --git a/DrissionPage/_units/clicker.py b/DrissionPage/_units/clicker.py index e490138..38cbd0b 100644 --- a/DrissionPage/_units/clicker.py +++ b/DrissionPage/_units/clicker.py @@ -32,7 +32,8 @@ class Clicker(object): if not by_js: # 模拟点击 can_click = False - timeout = self._ele.owner.timeout if timeout is None else timeout + if timeout is None: + timeout = self._ele.timeout rect = None if timeout == 0: try: diff --git a/DrissionPage/_units/selector.py b/DrissionPage/_units/selector.py index 52d8883..d6d3cfe 100644 --- a/DrissionPage/_units/selector.py +++ b/DrissionPage/_units/selector.py @@ -18,7 +18,8 @@ class SelectElement(object): def __call__(self, text_or_index, timeout=None): para_type = 'index' if isinstance(text_or_index, int) else 'text' - timeout = timeout if timeout is not None else self._ele.owner.timeout + if timeout is None: + timeout = self._ele.timeout return self._select(text_or_index, para_type, timeout=timeout) @property @@ -106,7 +107,8 @@ class SelectElement(object): raise TypeError('单选列表只能传入str格式。') mode = 'false' if cancel else 'true' - timeout = timeout if timeout is not None else self._ele.owner.timeout + if timeout is None: + timeout = self._ele.timeout condition = set(condition) if isinstance(condition, (list, tuple)) else {condition} if para_type in ('text', 'value'): diff --git a/DrissionPage/_units/waiter.py b/DrissionPage/_units/waiter.py index e536721..0bdabfc 100644 --- a/DrissionPage/_units/waiter.py +++ b/DrissionPage/_units/waiter.py @@ -9,7 +9,7 @@ from time import sleep, perf_counter from .._functions.locator import get_loc from .._functions.settings import Settings -from ..errors import WaitTimeoutError, NoRectError, ContextLostError +from ..errors import WaitTimeoutError, NoRectError class OriginWaiter(object): @@ -31,7 +31,8 @@ class BrowserWaiter(OriginWaiter): curr_tab = self._owner.tab_ids[0] elif hasattr(curr_tab, '_type'): curr_tab = curr_tab.tab_id - timeout = timeout if timeout is not None else self._owner.timeout + if timeout is None: + timeout = self._owner.timeout end_time = perf_counter() + timeout while perf_counter() < end_time: latest_tid = self._owner.tab_ids[0] @@ -151,7 +152,8 @@ class BaseWaiter(OriginWaiter): else [get_loc(x)[1] for x in locators]) method = any if any_one else all - timeout = self._owner.timeout if timeout is None else timeout + if timeout is None: + timeout = self._owner.timeout end_time = perf_counter() + timeout while perf_counter() < end_time: if method([_find(l, self._owner.driver) for l in locators]): @@ -302,7 +304,7 @@ class ElementWaiter(OriginWaiter): @property def _timeout(self): - return self._ele.owner.timeout + return self._ele.timeout def deleted(self, timeout=None, raise_err=None): return self._wait_state('is_alive', False, timeout, raise_err, err_text='等待元素被删除失败。')