From acc8a7fd6ef9caf0798e31571ec54bff6cf5d4e4 Mon Sep 17 00:00:00 2001 From: g1879 Date: Sat, 30 Mar 2024 21:55:19 +0800 Subject: [PATCH] =?UTF-8?q?4.0.4.18=E5=8F=96=E6=B6=88=E6=97=A0=E7=95=8C?= =?UTF-8?q?=E9=9D=A2Linux=E8=87=AA=E5=8A=A8=E6=97=A0=E5=A4=B4=EF=BC=9B?= =?UTF-8?q?=E4=BC=98=E5=8C=96wait.eles=5Floaded()=EF=BC=9B=E4=BF=AE?= =?UTF-8?q?=E5=A4=8Dprompt=E6=97=A0=E6=B3=95=E8=BE=93=E5=85=A5=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9B=E4=BF=AE=E5=A4=8DWebPageTab?= =?UTF-8?q?=E7=9A=84close()=E6=96=B9=E6=B3=95=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/__init__.py | 2 +- DrissionPage/_functions/browser.py | 12 ++++---- DrissionPage/_pages/chromium_base.py | 2 +- DrissionPage/_pages/chromium_tab.py | 3 ++ DrissionPage/_units/waiter.py | 45 ++++++++++++++++++++-------- DrissionPage/_units/waiter.pyi | 2 +- 6 files changed, 45 insertions(+), 21 deletions(-) diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 2a7edb8..634be2c 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] -__version__ = '4.0.4.17' +__version__ = '4.0.4.18' diff --git a/DrissionPage/_functions/browser.py b/DrissionPage/_functions/browser.py index ccc78fc..d0829b8 100644 --- a/DrissionPage/_functions/browser.py +++ b/DrissionPage/_functions/browser.py @@ -94,12 +94,12 @@ def get_launch_args(opt): opt.set_user_data_path(path) result.add(f'--user-data-dir={path}') - if headless is None and system().lower() == 'linux': - from os import popen - r = popen('systemctl list-units | grep graphical.target') - if 'graphical.target' not in r.read(): - headless = True - result.add('--headless=new') + # if headless is None and system().lower() == 'linux': # 无界面Linux自动加入无头 + # from os import popen + # r = popen('systemctl list-units | grep graphical.target') + # if 'graphical.target' not in r.read(): + # headless = True + # result.add('--headless=new') result = list(result) opt._headless = headless diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index c93ad8f..0caee3b 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -893,7 +893,7 @@ class ChromiumBase(BasePage): """alert出现时触发的方法""" self._alert.activated = True self._alert.text = kwargs['message'] - self._alert.type = kwargs['message'] + self._alert.type = kwargs['type'] self._alert.defaultPrompt = kwargs.get('defaultPrompt', None) self._alert.response_accept = None self._alert.response_text = None diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index fa0fb06..e496728 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -106,6 +106,9 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): :param page: WebPage对象 :param tab_id: 要控制的标签页id """ + if Settings.singleton_tab_obj and hasattr(self, '_created'): + return + self._mode = 'd' self._has_driver = True self._has_session = True diff --git a/DrissionPage/_units/waiter.py b/DrissionPage/_units/waiter.py index a67e032..b8a1bc5 100644 --- a/DrissionPage/_units/waiter.py +++ b/DrissionPage/_units/waiter.py @@ -7,6 +7,7 @@ """ from time import sleep, perf_counter +from .._functions.locator import get_loc from .._functions.settings import Settings from ..errors import WaitTimeoutError, NoRectError @@ -81,27 +82,47 @@ class BaseWaiter(OriginWaiter): return ele.wait.hidden(timeout, raise_err=raise_err) def eles_loaded(self, locators, timeout=None, any_one=False, raise_err=None): - """等待元素加载到DOM + """等待元素加载到DOM,可等待全部或任意一个 :param locators: 要等待的元素,输入定位符,用list输入多个 :param timeout: 超时时间,默认读取页面超时时间 :param any_one: 是否等待到一个就返回 :param raise_err: 等待失败时是否报错,为None时根据Settings设置 :return: 成功返回True,失败返回False """ - if isinstance(locators, (str, tuple)): - locators = (locators,) + + def _find(loc, driver): + r = driver.run('DOM.performSearch', query=loc, includeUserAgentShadowDOM=True) + if not r or 'error' in r: + return False + elif r['resultCount'] == 0: + driver.run('DOM.discardSearchResults', searchId=r['searchId']) + return False + searchId = r['searchId'] + ids = driver.run('DOM.getSearchResults', searchId=r['searchId'], fromIndex=0, + toIndex=r['resultCount'] - 1)['nodeIds'] + res = False + for i in ids: + r = driver.run('DOM.describeNode', nodeId=i) + if 'error' in r or r['node']['nodeName'] in ('#text', '#comment'): + continue + else: + res = True + break + driver.run('DOM.discardSearchResults', searchId=searchId) + return res + + by = ('id', 'xpath', 'link text', 'partial link text', 'name', 'tag name', 'class name', 'css selector') + locators = ((get_loc(locators)[1],) if (isinstance(locators, str) or isinstance(locators, tuple) + and locators[0] in by and len(locators) == 2) + else [get_loc(l)[1] for l in locators]) timeout = self._driver.timeout if timeout is None else timeout end_time = perf_counter() + timeout - if any_one: - while perf_counter() < end_time: - if any([self._driver._ele(l, raise_err=False, timeout=0)for l in locators]): - return True - else: - while perf_counter() < end_time: - if all([self._driver._ele(l, raise_err=False, timeout=0) for l in locators]): - return True + method = any if any_one else all + while perf_counter() < end_time: + if method([_find(l, self._driver.driver) for l in locators]): + return True if raise_err is True or Settings.raise_when_wait_failed is True: - raise WaitTimeoutError(f'等待元素加载失败(等待{timeout}秒)。') + raise WaitTimeoutError(f'等待元素{locators}加载失败(等待{timeout}秒)。') else: return False diff --git a/DrissionPage/_units/waiter.pyi b/DrissionPage/_units/waiter.pyi index d5c5895..4b96890 100644 --- a/DrissionPage/_units/waiter.pyi +++ b/DrissionPage/_units/waiter.pyi @@ -38,7 +38,7 @@ class BaseWaiter(OriginWaiter): raise_err: bool = None) -> bool: ... def eles_loaded(self, - locators: Union[Tuple[str, str], str], + locators: Union[Tuple[str, str], str, list, tuple], timeout: float = None, any_one: bool = False, raise_err: bool = None) -> bool: ...