From 545b7a0732ca387a3f5d8b810a2b62ead94962cd Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 14 Dec 2023 00:45:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E4=BA=9B=E4=BF=AE=E6=94=B9(+)=20fulls?= =?UTF-8?q?creen()=E6=94=B9=E4=B8=BAfull()=EF=BC=9B=20corners=E8=BF=94?= =?UTF-8?q?=E5=9B=9Etuple=EF=BC=9B=20Frame=E5=A2=9E=E5=8A=A0states.is=5Fdi?= =?UTF-8?q?splayed=E5=92=8Cstates.is=5Floading=EF=BC=9B=20has=5Falert?= =?UTF-8?q?=E7=A7=BB=E5=88=B0states=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_elements/chromium_element.py | 4 ++-- DrissionPage/_pages/chromium_base.py | 5 ----- DrissionPage/_pages/chromium_base.pyi | 3 --- DrissionPage/_pages/web_page.py | 2 +- DrissionPage/_pages/web_page.pyi | 2 ++ DrissionPage/_units/rect.py | 9 ++++----- DrissionPage/_units/rect.pyi | 8 ++++---- DrissionPage/_units/setter.py | 6 +++++- DrissionPage/_units/setter.pyi | 2 +- DrissionPage/_units/states.py | 18 ++++++++++++++++++ DrissionPage/_units/states.pyi | 6 ++++++ 11 files changed, 43 insertions(+), 22 deletions(-) diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index 71353d8..ebc6f7d 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -1287,7 +1287,7 @@ def run_js(page_or_ele, script, as_expr=False, timeout=None, args=None): obj_id = page_or_ele._root_id is_page = True - if page.has_alert: + if page.states.has_alert: raise AlertExistsError try: @@ -1309,7 +1309,7 @@ def run_js(page_or_ele, script, as_expr=False, timeout=None, args=None): else: raise ElementLostError('原来获取到的元素对象已不在页面内。') - if res is None and page.driver.has_alert: # 存在alert的情况 + if res is None and page.states.has_alert: # 存在alert的情况 return None exceptionDetails = res.get('exceptionDetails') diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index a48db70..437502c 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -426,11 +426,6 @@ class ChromiumBase(BasePage): """返回等待上传文件列表""" return self._upload_list - @property - def has_alert(self): - """返回是否存在提示框""" - return self._has_alert - @property def _js_ready_state(self): """返回js获取的ready state信息""" diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index 3624a20..e9acefc 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -162,9 +162,6 @@ class ChromiumBase(BasePage): @property def states(self) -> PageStates: ... - @property - def has_alert(self) -> bool: ... - def run_js(self, script: str, *args, as_expr: bool = False, timeout: float = None) -> Any: ... def run_js_loaded(self, script: str, *args, as_expr: bool = False, timeout: float = None) -> Any: ... diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index 3b08d1a..505f06d 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -349,7 +349,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): self._has_session = None def close(self): - """关闭标签页""" + """关闭标签页和Session""" if self._has_driver: self.close_tabs(self.tab_id) if self._session: diff --git a/DrissionPage/_pages/web_page.pyi b/DrissionPage/_pages/web_page.pyi index 7931c75..6b238e4 100644 --- a/DrissionPage/_pages/web_page.pyi +++ b/DrissionPage/_pages/web_page.pyi @@ -131,6 +131,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def close_session(self) -> None: ... + def close(self) -> None: ... + # ----------------重写SessionPage的函数----------------------- def post(self, url: str, diff --git a/DrissionPage/_units/rect.py b/DrissionPage/_units/rect.py index 0b9f0f0..93ac879 100644 --- a/DrissionPage/_units/rect.py +++ b/DrissionPage/_units/rect.py @@ -19,14 +19,13 @@ class ElementRect(object): r = self._ele.page.run_cdp_loaded('Page.getLayoutMetrics')['visualViewport'] sx = r['pageX'] sy = r['pageY'] - return [(vr[0] + sx, vr[1] + sy), (vr[2] + sx, vr[3] + sy), - (vr[4] + sx, vr[5] + sy), (vr[6] + sx, vr[7] + sy)] + return [(vr[0] + sx, vr[1] + sy), (vr[2] + sx, vr[3] + sy), (vr[4] + sx, vr[5] + sy), (vr[6] + sx, vr[7] + sy)] @property def viewport_corners(self): """返回元素四个角视口坐标,顺序:坐上、右上、右下、左下,没有大小的元素抛出NoRectError""" r = self._get_viewport_rect('border') - return [(r[0], r[1]), (r[2], r[3]), (r[4], r[5]), (r[6], r[7])] + return (r[0], r[1]), (r[2], r[3]), (r[4], r[5]), (r[6], r[7]) @property def size(self): @@ -188,12 +187,12 @@ class FrameRect(object): @property def location(self): - """返回元素左上角的绝对坐标""" + """返回iframe元素左上角的绝对坐标""" return self._frame.frame_ele.rect.location @property def viewport_location(self): - """返回视口在屏幕中坐标,左上角为(0, 0)""" + """返回元素在视口中坐标,左上角为(0, 0)""" return self._frame.frame_ele.rect.viewport_location @property diff --git a/DrissionPage/_units/rect.pyi b/DrissionPage/_units/rect.pyi index b45ef98..deb346f 100644 --- a/DrissionPage/_units/rect.pyi +++ b/DrissionPage/_units/rect.pyi @@ -49,10 +49,10 @@ class ElementRect(object): def screen_click_point(self) -> Tuple[float, float]: ... @property - def corners(self) -> List[Tuple[float, float], ...]: ... + def corners(self) -> Tuple[Tuple[float, float], ...]: ... @property - def viewport_corners(self) -> List[Tuple[float, float], ...]: ... + def viewport_corners(self) -> Tuple[Tuple[float, float], ...]: ... def _get_viewport_rect(self, quad: str) -> Union[list, None]: ... @@ -112,7 +112,7 @@ class FrameRect(object): def viewport_size(self) -> Tuple[float, float]: ... @property - def corners(self) -> List[Tuple[float, float], ...]: ... + def corners(self) -> Tuple[Tuple[float, float], ...]: ... @property - def viewport_corners(self) -> List[Tuple[float, float], ...]: ... + def viewport_corners(self) -> Tuple[Tuple[float, float], ...]: ... diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 4b5b952..7d5486b 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -572,7 +572,7 @@ class WindowSetter(object): self._perform({'windowState': 'normal'}) self._perform({'windowState': 'minimized'}) - def fullscreen(self): + def full(self): """设置窗口为全屏""" s = self._get_info()['bounds']['windowState'] if s == 'minimized': @@ -635,6 +635,10 @@ class WindowSetter(object): """窗口最小化""" self.mini() + def fullscreen(self): + """设置窗口为全屏""" + self.full() + class PageWindowSetter(WindowSetter): def hide(self): diff --git a/DrissionPage/_units/setter.pyi b/DrissionPage/_units/setter.pyi index 19e2196..582e1af 100644 --- a/DrissionPage/_units/setter.pyi +++ b/DrissionPage/_units/setter.pyi @@ -207,7 +207,7 @@ class WindowSetter(object): def mini(self) -> None: ... - def fullscreen(self) -> None: ... + def full(self) -> None: ... def normal(self) -> None: ... diff --git a/DrissionPage/_units/states.py b/DrissionPage/_units/states.py index dc4a020..a53da2a 100644 --- a/DrissionPage/_units/states.py +++ b/DrissionPage/_units/states.py @@ -132,6 +132,11 @@ class PageStates(object): """返回当前页面加载状态,'loading' 'interactive' 'complete'""" return self._page._ready_state + @property + def has_alert(self): + """返回当前页面加载状态,'loading' 'interactive' 'complete'""" + return self._page._has_alert + class FrameStates(object): def __init__(self, frame): @@ -140,6 +145,11 @@ class FrameStates(object): """ self._frame = frame + @property + def is_loading(self): + """返回页面是否在加载状态""" + return self._frame._is_loading + @property def is_alive(self): """返回frame元素是否可用,且里面仍挂载有frame""" @@ -152,4 +162,12 @@ class FrameStates(object): @property def ready_state(self): + """返回加载状态""" return self._frame._ready_state + + @property + def is_displayed(self): + """返回iframe是否显示""" + return not (self._frame.frame_ele.style('visibility') == 'hidden' + or self._frame.frame_ele.run_js('return this.offsetParent === null;') + or self._frame.frame_ele.style('display') == 'none') diff --git a/DrissionPage/_units/states.pyi b/DrissionPage/_units/states.pyi index e3c303c..0bad17b 100644 --- a/DrissionPage/_units/states.pyi +++ b/DrissionPage/_units/states.pyi @@ -69,11 +69,17 @@ class PageStates(object): @property def ready_state(self) -> Optional[str]: ... + @property + def has_alert(self) -> bool: ... + class FrameStates(object): def __init__(self, frame: ChromiumFrame): self._frame: ChromiumFrame = ... + @property + def is_loading(self) -> bool: ... + @property def is_alive(self) -> bool: ...