From 722e2991506442cc37ed691cf61af81de8e8b864 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 30 Oct 2023 22:05:52 +0800 Subject: [PATCH] =?UTF-8?q?`rect.borwser=5Fsize`=E6=94=B9=E4=B8=BA`window?= =?UTF-8?q?=5Fsize`=EF=BC=9B`rect.borwser=5Flocation`=E6=94=B9=E4=B8=BA`wi?= =?UTF-8?q?ndow=5Flocation`=EF=BC=9Bcdp=E8=AE=BE=E7=BD=AE30=E7=A7=92?= =?UTF-8?q?=E8=B6=85=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/chromium_driver.py | 2 +- DrissionPage/_pages/chromium_base.py | 44 ++++++++++++------------ DrissionPage/_pages/chromium_base.pyi | 1 + DrissionPage/_units/network_listener.pyi | 3 +- DrissionPage/_units/setter.py | 10 +++--- DrissionPage/_units/setter.pyi | 10 +++--- DrissionPage/_units/tab_rect.py | 38 ++++++++++---------- DrissionPage/_units/tab_rect.pyi | 6 ++-- 8 files changed, 58 insertions(+), 56 deletions(-) diff --git a/DrissionPage/_base/chromium_driver.py b/DrissionPage/_base/chromium_driver.py index b6781e2..948cdc7 100644 --- a/DrissionPage/_base/chromium_driver.py +++ b/DrissionPage/_base/chromium_driver.py @@ -155,7 +155,7 @@ class ChromiumDriver(object): if self._stopped.is_set(): return {'error': 'tab closed', 'type': 'tab_closed'} - timeout = kwargs.pop("_timeout", None) + timeout = kwargs.pop("_timeout", 30) result = self._send({"method": _method, "params": kwargs}, timeout=timeout) if result is None: return {'error': 'tab closed', 'type': 'tab_closed'} diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 1adf588..2b0d462 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -145,28 +145,6 @@ class ChromiumBase(BasePage): self._is_loading = False self._is_reading = False - def _wait_loaded(self, timeout=None): - """等待页面加载完成,超时触发停止加载 - :param timeout: 超时时间 - :return: 是否成功,超时返回False - """ - if self.page_load_strategy == 'none': - return True - - timeout = timeout if timeout is not None else self.timeouts.page_load - end_time = perf_counter() + timeout - while perf_counter() < end_time: - if self._ready_state == 'complete': - return True - elif self.page_load_strategy == 'eager' and self._ready_state in ('interactive', 'complete'): - self.stop_loading() - return True - - sleep(.1) - - self.stop_loading() - return False - def _onFrameDetached(self, **kwargs): try: self.browser._frames.pop(kwargs['frameId']) @@ -845,6 +823,28 @@ class ChromiumBase(BasePage): self._alert.response_text = None self._has_alert = True + def _wait_loaded(self, timeout=None): + """等待页面加载完成,超时触发停止加载 + :param timeout: 超时时间 + :return: 是否成功,超时返回False + """ + if self.page_load_strategy == 'none': + return True + + timeout = timeout if timeout is not None else self.timeouts.page_load + end_time = perf_counter() + timeout + while perf_counter() < end_time: + if self._ready_state == 'complete': + return True + elif self.page_load_strategy == 'eager' and self._ready_state in ('interactive', + 'complete') and not self._is_loading: + return True + + sleep(.1) + + self.stop_loading() + return False + def _d_connect(self, to_url, times=0, interval=1, show_errmsg=False, timeout=None): """尝试连接,重试若干次 :param to_url: 要访问的url diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index 7c34135..8c35695 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -32,6 +32,7 @@ class ChromiumBase(BasePage): self._driver: ChromiumDriver = ... self._frame_id: str = ... self._is_reading: bool = ... + self._is_timeout: bool = ... self._timeouts: Timeout = ... self._first_run: bool = ... self._is_loading: bool = ... diff --git a/DrissionPage/_units/network_listener.pyi b/DrissionPage/_units/network_listener.pyi index bb26f99..75aab24 100644 --- a/DrissionPage/_units/network_listener.pyi +++ b/DrissionPage/_units/network_listener.pyi @@ -35,7 +35,8 @@ class NetworkListener(object): def go_on(self) -> None: ... - def wait(self, count: int = 1, timeout: float = None, fix_count: bool = True): ... + def wait(self, count: int = 1, timeout: float = None, + fix_count: bool = True) -> Union[List[DataPacket], DataPacket, None]: ... @property def results(self) -> Union[DataPacket, Dict[str, List[DataPacket]], False]: ... diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index f0de74a..841aea3 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -122,6 +122,11 @@ class TabSetter(ChromiumBaseSetter): def __init__(self, page): super().__init__(page) + @property + def window(self): + """返回用于设置浏览器窗口的对象""" + return WindowSetter(self._page) + def download_path(self, path): """设置下载路径 :param path: 下载路径 @@ -165,11 +170,6 @@ class ChromiumPageSetter(TabSetter): """ self._page._main_tab = tab_id or self._page.tab_id - @property - def window(self): - """返回用于设置浏览器窗口的对象""" - return WindowSetter(self._page) - def tab_to_front(self, tab_or_id=None): """激活标签页使其处于最前面 :param tab_or_id: 标签页对象或id,为None表示当前标签页 diff --git a/DrissionPage/_units/setter.pyi b/DrissionPage/_units/setter.pyi index f44413e..e98b7de 100644 --- a/DrissionPage/_units/setter.pyi +++ b/DrissionPage/_units/setter.pyi @@ -56,6 +56,9 @@ class ChromiumBaseSetter(object): class TabSetter(ChromiumBaseSetter): def __init__(self, page): ... + @property + def window(self) -> WindowSetter: ... + def download_path(self, path: Union[str, Path]) -> None: ... def download_file_name(self, name: str) -> None: ... @@ -70,9 +73,6 @@ class ChromiumPageSetter(TabSetter): def main_tab(self, tab_id: str = None) -> None: ... - @property - def window(self) -> WindowSetter: ... - def tab_to_front(self, tab_or_id: Union[str, ChromiumTab] = None) -> None: ... @@ -184,8 +184,8 @@ class PageScrollSetter(object): class WindowSetter(object): - def __init__(self, page: ChromiumPage): - self._page: ChromiumPage = ... + def __init__(self, page: ChromiumBase): + self._page: ChromiumBase = ... self._window_id: str = ... def maximized(self) -> None: ... diff --git a/DrissionPage/_units/tab_rect.py b/DrissionPage/_units/tab_rect.py index 6655932..df27036 100644 --- a/DrissionPage/_units/tab_rect.py +++ b/DrissionPage/_units/tab_rect.py @@ -12,16 +12,27 @@ class ChromiumTabRect(object): @property def window_state(self): """返回窗口状态:normal、fullscreen、maximized、 minimized""" - return self._get_browser_rect()['windowState'] + return self._get_window_rect()['windowState'] @property - def browser_location(self): - """返回浏览器在屏幕上的坐标,左上角为(0, 0)""" - r = self._get_browser_rect() + def window_location(self): + """返回窗口在屏幕上的坐标,左上角为(0, 0)""" + r = self._get_window_rect() if r['windowState'] in ('maximized', 'fullscreen'): return 0, 0 return r['left'] + 7, r['top'] + @property + def window_size(self): + """返回窗口大小""" + r = self._get_window_rect() + if r['windowState'] == 'fullscreen': + return r['width'], r['height'] + elif r['windowState'] == 'maximized': + return r['width'] - 16, r['height'] - 16 + else: + return r['width'] - 16, r['height'] - 7 + @property def page_location(self): """返回页面左上角在屏幕中坐标,左上角为(0, 0)""" @@ -32,22 +43,11 @@ class ChromiumTabRect(object): @property def viewport_location(self): """返回视口在屏幕中坐标,左上角为(0, 0)""" - w_bl, h_bl = self.browser_location - w_bs, h_bs = self.browser_size + w_bl, h_bl = self.window_location + w_bs, h_bs = self.window_size w_vs, h_vs = self.viewport_size_with_scrollbar return w_bl + w_bs - w_vs, h_bl + h_bs - h_vs - @property - def browser_size(self): - """返回浏览器大小""" - r = self._get_browser_rect() - if r['windowState'] == 'fullscreen': - return r['width'], r['height'] - elif r['windowState'] == 'maximized': - return r['width'] - 16, r['height'] - 16 - else: - return r['width'] - 16, r['height'] - 7 - @property def page_size(self): """返回页面总宽高,格式:(宽, 高)""" @@ -71,6 +71,6 @@ class ChromiumTabRect(object): """获取页面范围信息""" return self._page.run_cdp_loaded('Page.getLayoutMetrics') - def _get_browser_rect(self): - """获取浏览器范围信息""" + def _get_window_rect(self): + """获取窗口范围信息""" return self._page.browser.get_window_bounds(self._page.tab_id) diff --git a/DrissionPage/_units/tab_rect.pyi b/DrissionPage/_units/tab_rect.pyi index 95ebadc..d21aa1e 100644 --- a/DrissionPage/_units/tab_rect.pyi +++ b/DrissionPage/_units/tab_rect.pyi @@ -17,7 +17,7 @@ class ChromiumTabRect(object): def window_state(self) -> str: ... @property - def browser_location(self) -> Tuple[int, int]: ... + def window_location(self) -> Tuple[int, int]: ... @property def page_location(self) -> Tuple[int, int]: ... @@ -26,7 +26,7 @@ class ChromiumTabRect(object): def viewport_location(self) -> Tuple[int, int]: ... @property - def browser_size(self) -> Tuple[int, int]: ... + def window_size(self) -> Tuple[int, int]: ... @property def page_size(self) -> Tuple[int, int]: ... @@ -39,4 +39,4 @@ class ChromiumTabRect(object): def _get_page_rect(self) -> dict: ... - def _get_browser_rect(self) -> dict: ... + def _get_window_rect(self) -> dict: ...