From e19b13734d0c3df66e62bee3533b22441153ae5a Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 3 Dec 2024 18:13:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=8D=E4=BD=8E=E5=BE=AA=E7=8E=AF=E7=AD=89?= =?UTF-8?q?=E5=BE=85=E9=97=B4=E9=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/chromium.py | 4 ++-- DrissionPage/_base/driver.py | 2 +- DrissionPage/_elements/chromium_element.py | 14 +++++++------- DrissionPage/_functions/cookies.py | 3 +-- DrissionPage/_pages/chromium_base.py | 14 +++++++------- DrissionPage/_pages/chromium_base.pyi | 5 +++-- DrissionPage/_pages/chromium_frame.py | 2 +- DrissionPage/_pages/chromium_page.py | 2 +- DrissionPage/_pages/chromium_tab.py | 2 +- DrissionPage/_units/listener.py | 8 ++++---- DrissionPage/_units/screencast.py | 4 ++-- DrissionPage/_units/scroller.py | 2 +- DrissionPage/_units/setter.py | 2 +- 13 files changed, 32 insertions(+), 32 deletions(-) diff --git a/DrissionPage/_base/chromium.py b/DrissionPage/_base/chromium.py index abf6b57..28e944c 100644 --- a/DrissionPage/_base/chromium.py +++ b/DrissionPage/_base/chromium.py @@ -45,7 +45,7 @@ class Chromium(object): if browser_id in cls._BROWSERS: r = cls._BROWSERS[browser_id] while not hasattr(r, '_driver'): - sleep(.1) + sleep(.05) return r r = object.__new__(cls) r._chromium_options = opt @@ -318,7 +318,7 @@ class Chromium(object): while self.states.is_alive: if tab in self._drivers: break - sleep(.1) + sleep(.01) else: raise BrowserConnectError('浏览器已关闭') tab = tab_type(self, tab) diff --git a/DrissionPage/_base/driver.py b/DrissionPage/_base/driver.py index 6374f6a..5b7c40b 100644 --- a/DrissionPage/_base/driver.py +++ b/DrissionPage/_base/driver.py @@ -199,7 +199,7 @@ class Driver(object): def stop(self): self._stop() while self._handle_event_th.is_alive() or self._recv_th.is_alive(): - sleep(.1) + sleep(.01) return True def _stop(self): diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index b6250db..22f8bc1 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -289,7 +289,7 @@ class ChromiumElement(DrissionElement): if ele and (loc_data is None or _check_ele(ele, loc_data)): return ele - sleep(.1) + sleep(.01) return NoneElement(page=self.owner, method='offset()', args={'locator': locator, 'offset_x': x, 'offset_y': y, 'timeout': timeout}) @@ -451,7 +451,7 @@ class ChromiumElement(DrissionElement): '&& this.naturalHeight > 0') end_time = perf_counter() + timeout while not self._run_js(js) and perf_counter() < end_time: - sleep(.1) + sleep(.05) src = self.attr('href') if self.tag == 'link' else self.attr('src') if not src: @@ -488,7 +488,7 @@ class ChromiumElement(DrissionElement): break except CDPError: pass - sleep(.1) + sleep(.05) if not result: return None @@ -533,7 +533,7 @@ class ChromiumElement(DrissionElement): '&& typeof this.naturalHeight != "undefined" && this.naturalHeight > 0') end_time = perf_counter() + self.timeout while not self._run_js(js) and perf_counter() < end_time: - sleep(.1) + sleep(.05) if scroll_to_center: self.scroll.to_see(center=True) @@ -905,7 +905,7 @@ class ShadowRoot(BaseElement): end_time = perf_counter() + timeout result = do_find() while result is None and perf_counter() <= end_time: - sleep(.1) + sleep(.01) result = do_find() if result: @@ -1004,7 +1004,7 @@ def find_by_xpath(ele, xpath, index, timeout, relative=True): end_time = perf_counter() + timeout result = do_find() while result is None and perf_counter() < end_time: - sleep(.1) + sleep(.01) result = do_find() if result: @@ -1043,7 +1043,7 @@ def find_by_css(ele, selector, index, timeout): end_time = perf_counter() + timeout result = do_find() while result is None and perf_counter() < end_time: - sleep(.1) + sleep(.01) result = do_find() if result: diff --git a/DrissionPage/_functions/cookies.py b/DrissionPage/_functions/cookies.py index 7092909..2a8d59e 100644 --- a/DrissionPage/_functions/cookies.py +++ b/DrissionPage/_functions/cookies.py @@ -92,7 +92,6 @@ def set_browser_cookies(browser, cookies): def set_tab_cookies(page, cookies): - suffixes_list = f"file:///{Settings.suffixes_list_path}" for cookie in cookies_to_tuple(cookies): cookie = format_cookie(cookie) @@ -117,7 +116,7 @@ def set_tab_cookies(page, cookies): if not url.startswith('http'): raise RuntimeError(f'未设置域名,请设置cookie的domain参数或先访问一个网站。{cookie}') ex_url = TLDExtract(suffix_list_urls=["https://publicsuffix.org/list/public_suffix_list.dat", - suffixes_list]).extract_str(url) + f"file:///{Settings.suffixes_list_path}"]).extract_str(url) d_list = ex_url.subdomain.split('.') d_list.append(f'{ex_url.domain}.{ex_url.suffix}' if ex_url.suffix else ex_url.domain) diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index ab332bd..e6ce36d 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -150,7 +150,7 @@ class ChromiumBase(BasePage): except: timeout = end_time - perf_counter() timeout = .5 if timeout <= 0 else timeout - sleep(.1) + sleep(.05) else: result = False @@ -223,7 +223,7 @@ class ChromiumBase(BasePage): def _wait_to_stop(self): end_time = perf_counter() + self.timeouts.page_load while perf_counter() < end_time: - sleep(.1) + sleep(.02) if self._ready_state in ('interactive', 'complete') and self._is_loading: self.stop_loading() @@ -486,7 +486,7 @@ class ChromiumBase(BasePage): if perf_counter() >= end_time: return NoneElement(self) if index is not None else ChromiumElementsList(owner=self) - sleep(.1) + sleep(.01) timeout = end_time - perf_counter() timeout = .5 if timeout <= 0 else timeout result = self.driver.run('DOM.performSearch', query=loc, _timeout=timeout, includeUserAgentShadowDOM=True) @@ -539,7 +539,7 @@ class ChromiumBase(BasePage): self._run_cdp('Page.stopLoading') end_time = perf_counter() + 5 while self._ready_state != 'complete' and perf_counter() < end_time: - sleep(.1) + sleep(.02) except (PageDisconnectedError, CDPError): pass finally: @@ -693,7 +693,7 @@ class ChromiumBase(BasePage): timeout = .1 if timeout <= 0 else timeout end_time = perf_counter() + timeout while not self._alert.activated and perf_counter() < end_time: - sleep(.1) + sleep(.01) if not self._alert.activated: return False @@ -744,7 +744,7 @@ class ChromiumBase(BasePage): 'complete') and not self._is_loading: return True - sleep(.1) + sleep(.01) try: self.stop_loading() @@ -773,7 +773,7 @@ class ChromiumBase(BasePage): print(f'重试{t + 1} {to_url}') end_time1 = end_time - perf_counter() while self._ready_state not in ('loading', 'complete') and perf_counter() < end_time1: # 等待出错信息显示 - sleep(.1) + sleep(.01) self.stop_loading() continue diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index b7bc592..1d2b9ce 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -267,7 +267,7 @@ class ChromiumBase(BasePage): ... @property - def session(self)->Session: + def session(self) -> Session: """返回用于转换模式或download的Session对象""" ... @@ -479,7 +479,8 @@ class ChromiumBase(BasePage): def add_ele(self, html_or_info: Union[str, Tuple[str, dict]], insert_to: Union[ChromiumElement, str, Tuple[str, str], None] = None, - before: Union[ChromiumElement, str, Tuple[str, str], None] = None) -> ChromiumElement: + before: Union[ChromiumElement, str, Tuple[str, str], None] = None) -> Union[ + ChromiumElement, ChromiumFrame]: """新建一个元素 :param html_or_info: 新元素的html文本或信息。信息格式为:(tag, {attr1: value, ...}) :param insert_to: 插入到哪个元素中,可接收元素对象和定位符,为None且为html添加到body,不为html不插入 diff --git a/DrissionPage/_pages/chromium_frame.py b/DrissionPage/_pages/chromium_frame.py index 8b02be3..a055d47 100644 --- a/DrissionPage/_pages/chromium_frame.py +++ b/DrissionPage/_pages/chromium_frame.py @@ -35,7 +35,7 @@ class ChromiumFrame(ChromiumBase): if Settings.singleton_tab_obj and fid in cls._Frames: r = cls._Frames[fid] while not hasattr(r, '_type') or r._type != 'ChromiumFrame': - sleep(.1) + sleep(.01) return r r = object.__new__(cls) cls._Frames[fid] = r diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index 76a2eee..7815249 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -24,7 +24,7 @@ class ChromiumPage(ChromiumBase): if browser.id in cls._PAGES: r = cls._PAGES[browser.id] while not hasattr(r, '_frame_id'): - sleep(.1) + sleep(.05) return r r = object.__new__(cls) diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index 6d4b62a..3b4f425 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -22,7 +22,7 @@ class ChromiumTab(ChromiumBase): if Settings.singleton_tab_obj and tab_id in cls._TABS: r = cls._TABS[tab_id] while not hasattr(r, '_frame_id'): - sleep(.1) + sleep(.05) return r r = object.__new__(cls) cls._TABS[tab_id] = r diff --git a/DrissionPage/_units/listener.py b/DrissionPage/_units/listener.py index 41d38ad..f3d3966 100644 --- a/DrissionPage/_units/listener.py +++ b/DrissionPage/_units/listener.py @@ -191,7 +191,7 @@ class Listener(object): if timeout is None: while ((not targets_only and self._running_requests > limit) or (targets_only and self._running_targets > limit)): - sleep(.1) + sleep(.01) return True end_time = perf_counter() + timeout @@ -199,7 +199,7 @@ class Listener(object): if ((not targets_only and self._running_requests <= limit) or (targets_only and self._running_targets <= limit)): return True - sleep(.1) + sleep(.01) else: return False @@ -423,7 +423,7 @@ class DataPacket(object): def wait_extra_info(self, timeout=None): if timeout is None: while self._responseExtraInfo is None: - sleep(.1) + sleep(.01) return True else: @@ -431,7 +431,7 @@ class DataPacket(object): while perf_counter() < end_time: if self._responseExtraInfo is not None: return True - sleep(.1) + sleep(.01) else: return False diff --git a/DrissionPage/_units/screencast.py b/DrissionPage/_units/screencast.py index b6cb152..b5bcaed 100644 --- a/DrissionPage/_units/screencast.py +++ b/DrissionPage/_units/screencast.py @@ -86,7 +86,7 @@ class Screencast(object): if self._mode.startswith('js'): self._owner._run_js('mediaRecorder.stop();', as_expr=True) while not self._owner._run_js('return DrissionPage_Screencast_blob_ok;'): - sleep(.1) + sleep(.05) blob = self._owner._run_js('return DrissionPage_Screencast_blob;') uuid = self._owner._run_cdp('IO.resolveBlob', objectId=blob['result']['objectId'])['uuid'] data = self._owner._run_cdp('IO.read', handle=f'blob:{uuid}')['data'] @@ -100,7 +100,7 @@ class Screencast(object): else: self._enable = False while self._running: - sleep(.1) + sleep(.01) if self._mode.endswith('imgs'): return str(Path(self._path).absolute()) diff --git a/DrissionPage/_units/scroller.py b/DrissionPage/_units/scroller.py index 0bb7b1c..66c6ce3 100644 --- a/DrissionPage/_units/scroller.py +++ b/DrissionPage/_units/scroller.py @@ -77,7 +77,7 @@ class Scroller(object): end_time = perf_counter() + owner.timeout while perf_counter() < end_time: - sleep(.1) + sleep(.02) r = owner._run_cdp('Page.getLayoutMetrics') x1 = r['layoutViewport']['pageX'] y1 = r['layoutViewport']['pageY'] diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 909d572..492e1a0 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -469,7 +469,7 @@ class WindowSetter(object): try: return self._owner._run_cdp('Browser.getWindowForTarget') except: - sleep(.1) + sleep(.02) raise RuntimeError('获取窗口信息失败。') def _perform(self, bounds):