From 727c005f19598381703ac46162d53b13c6b538ee Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 7 Mar 2024 17:41:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E6=9C=AC=E7=94=9F?= =?UTF-8?q?=E6=88=90=E7=9A=84=E9=9D=99=E6=80=81=E5=85=83=E7=B4=A0link?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98=EF=BC=9B=E5=BE=AE=E8=B0=83?= =?UTF-8?q?=E7=94=9F=E6=88=90=E9=9D=99=E6=80=81=E5=85=83=E7=B4=A0=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/browser.py | 5 ++++- DrissionPage/_elements/chromium_element.py | 11 +++-------- DrissionPage/_elements/session_element.py | 10 +++++++--- DrissionPage/_pages/chromium_base.py | 2 +- DrissionPage/_pages/chromium_base.pyi | 3 +-- DrissionPage/_pages/session_page.py | 2 +- DrissionPage/_units/rect.py | 8 +++++++- DrissionPage/_units/rect.pyi | 2 +- 8 files changed, 25 insertions(+), 18 deletions(-) diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index 4ab366d..92b8b6b 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -213,7 +213,10 @@ class Browser(object): if force: from psutil import Process for pid in pids: - Process(pid).kill() + try: + Process(pid).kill() + except: + pass else: try: self.run_cdp('Browser.close') diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index ab52c10..3f7eca7 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -123,7 +123,7 @@ class ChromiumElement(DrissionElement): @property def text(self): """返回元素内所有文本,文本已格式化""" - return get_ele_txt(make_session_ele(self.html)) + return get_ele_txt(make_session_ele(self)) @property def raw_text(self): @@ -447,10 +447,7 @@ class ChromiumElement(DrissionElement): :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :return: SessionElement对象或属性、文本 """ - if self.tag in __FRAME_ELEMENT__: - r = make_session_ele(self.inner_html, locator, index=index) - else: - r = make_session_ele(self, locator, index=index) + r = make_session_ele(self, locator, index=index) if isinstance(r, NoneElement): if Settings.raise_when_ele_not_found: raise ElementNotFoundError(None, 's_ele()', {'locator': locator}) @@ -464,8 +461,6 @@ class ChromiumElement(DrissionElement): :param locator: 定位符 :return: SessionElement或属性、文本组成的列表 """ - if self.tag in __FRAME_ELEMENT__: - return make_session_ele(self.inner_html, locator, index=None) return make_session_ele(self, locator, index=None) def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None): @@ -1080,7 +1075,7 @@ class ShadowRoot(BaseElement): return None if r is False else r else: - eles = make_session_ele(self.html).eles(loc) + eles = make_session_ele(self, loc, index=None) if not eles: return None diff --git a/DrissionPage/_elements/session_element.py b/DrissionPage/_elements/session_element.py index b2479c8..07c2a87 100644 --- a/DrissionPage/_elements/session_element.py +++ b/DrissionPage/_elements/session_element.py @@ -200,12 +200,12 @@ class SessionElement(DrissionElement): # 若为链接为None、js或邮件,直接返回 if not link or link.lower().startswith(('javascript:', 'mailto:')): return link - else: # 其它情况直接返回绝对url - return make_absolute_link(link, self.owner.url) + return make_absolute_link(link, self.owner.url) if self.owner else link elif name == 'src': - return make_absolute_link(self.inner_ele.get('src'), self.owner.url) + return make_absolute_link(self.inner_ele.get('src'), + self.owner.url) if self.owner else self.inner_ele.get('src') elif name == 'text': return self.text @@ -351,6 +351,10 @@ def make_session_ele(html_or_ele, loc=None, index=1): html_or_ele = fromstring(html) html_or_ele = html_or_ele.xpath(xpath)[0] + elif html_or_ele._type == 'ChromiumFrame': + page = html_or_ele + html_or_ele = fromstring(html_or_ele.inner_html) + # 各种页面对象 elif isinstance(html_or_ele, BasePage): page = html_or_ele diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 9c671a3..6ee9b4f 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -162,7 +162,7 @@ class ChromiumBase(BasePage): try: b_id = self.run_cdp('DOM.getDocument', _timeout=timeout)['root']['backendNodeId'] timeout = end_time - perf_counter() - timeout = .5 if timeout <= 0 else timeout + timeout = 1 if timeout <= 1 else timeout self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id, _timeout=timeout)['object']['objectId'] result = True diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index 59be1c2..fe8050f 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -8,8 +8,7 @@ from pathlib import Path from typing import Union, Tuple, List, Any, Optional, Literal -from .chromium_tab import ChromiumTab, WebPageTab -from .web_page import WebPage +from .chromium_tab import ChromiumTab from .._base.base import BasePage from .._base.browser import Browser from .._base.driver import Driver diff --git a/DrissionPage/_pages/session_page.py b/DrissionPage/_pages/session_page.py index b739545..d0e81a3 100644 --- a/DrissionPage/_pages/session_page.py +++ b/DrissionPage/_pages/session_page.py @@ -200,7 +200,7 @@ class SessionPage(BasePage): :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :return: SessionElement对象或属性、文本 """ - return make_session_ele(self.html) if locator is None else self._ele(locator, index=index, method='s_ele()') + return make_session_ele(self) if locator is None else self._ele(locator, index=index, method='s_ele()') def s_eles(self, locator): """返回页面中符合条件的所有元素、属性或节点文本 diff --git a/DrissionPage/_units/rect.py b/DrissionPage/_units/rect.py index c6f26ee..3628f94 100644 --- a/DrissionPage/_units/rect.py +++ b/DrissionPage/_units/rect.py @@ -114,11 +114,14 @@ class ElementRect(object): class TabRect(object): def __init__(self, owner): + """ + :param owner: Page对象和Tab对象 + """ self._owner = owner @property def window_state(self): - """返回窗口状态:normal、fullscreen、maximized、 minimized""" + """返回窗口状态:normal、fullscreen、maximized、minimized""" return self._get_window_rect()['windowState'] @property @@ -187,6 +190,9 @@ class FrameRect(object): """异域iframe使用""" def __init__(self, frame): + """ + :param frame: ChromiumFrame对象 + """ self._frame = frame @property diff --git a/DrissionPage/_units/rect.pyi b/DrissionPage/_units/rect.pyi index 4de0caf..4fa4e73 100644 --- a/DrissionPage/_units/rect.pyi +++ b/DrissionPage/_units/rect.pyi @@ -62,7 +62,7 @@ class ElementRect(object): class TabRect(object): - def __init__(self, owner: Union[ChromiumPage, ChromiumTab, WebPage, WebPageTab]): + def __init__(self, owner: ChromiumBase): self._owner: Union[ChromiumPage, ChromiumTab, WebPage, WebPageTab] = ... @property