From 58285fc0adfb477eba479665820aa56c8b9b3079 Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 9 Jul 2024 21:43:12 +0800 Subject: [PATCH] =?UTF-8?q?4.1.0.0b3=E4=BF=AE=E5=A4=8D=E6=B8=85=E7=A9=BA?= =?UTF-8?q?=E5=92=8C=E5=88=A0=E9=99=A4cookies=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/__init__.py | 2 +- DrissionPage/_units/cookies_setter.py | 40 +++++++------ DrissionPage/_units/cookies_setter.pyi | 18 +++--- DrissionPage/_units/waiter.py | 82 +++++++++----------------- 4 files changed, 61 insertions(+), 81 deletions(-) diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index e22b175..2fcdf31 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -14,4 +14,4 @@ from ._pages.chromium_page import ChromiumPage from ._pages.mix_page import MixPage from ._pages.mix_page import MixPage as WebPage -__version__ = '4.1.0.0b2' +__version__ = '4.1.0.0b3' diff --git a/DrissionPage/_units/cookies_setter.py b/DrissionPage/_units/cookies_setter.py index cc100c2..dd9764c 100644 --- a/DrissionPage/_units/cookies_setter.py +++ b/DrissionPage/_units/cookies_setter.py @@ -8,13 +8,27 @@ from .._functions.cookies import set_tab_cookies, set_session_cookies, set_browser_cookies -class CookiesSetter(object): +class BrowserCookiesSetter(object): def __init__(self, owner): """ - :param owner: ChromiumBase对象 + :param owner: Chromium对象 """ self._owner = owner + def __call__(self, cookies): + """设置一个或多个cookie + :param cookies: cookies信息 + :return: None + """ + set_browser_cookies(self._owner, cookies) + + def clear(self): + """清除cookies""" + self._owner._run_cdp('Storage.clearCookies') + + +class CookiesSetter(BrowserCookiesSetter): + def __call__(self, cookies): """设置一个或多个cookie :param cookies: cookies信息 @@ -22,6 +36,10 @@ class CookiesSetter(object): """ set_tab_cookies(self._owner, cookies) + def clear(self): + """清除cookies""" + self._owner._run_cdp('Network.clearBrowserCookies') + def remove(self, name, url=None, domain=None, path=None): """删除一个cookie :param name: cookie的name字段 @@ -37,23 +55,11 @@ class CookiesSetter(object): d['domain'] = domain if not url and not domain: d['url'] = self._owner.url + if not d['url'].startswith('http'): + raise ValueError('需设置domain或url值。如设置url值,需以http开头。') if path is not None: d['path'] = path - self._owner._run_cdp('Storage.deleteCookies', **d) - - def clear(self): - """清除cookies""" - self._owner._run_cdp('Storage.clearBrowserCookies') - - -class BrowserCookiesSetter(CookiesSetter): - - def __call__(self, cookies): - """设置一个或多个cookie - :param cookies: cookies信息 - :return: None - """ - set_browser_cookies(self._owner, cookies) + self._owner._run_cdp('Network.deleteCookies', **d) class SessionCookiesSetter(object): diff --git a/DrissionPage/_units/cookies_setter.pyi b/DrissionPage/_units/cookies_setter.pyi index 3eb6c40..8ce3d58 100644 --- a/DrissionPage/_units/cookies_setter.pyi +++ b/DrissionPage/_units/cookies_setter.pyi @@ -15,22 +15,24 @@ from .._pages.session_page import SessionPage from .._pages.mix_page import MixPage -class CookiesSetter(object): - _owner: ChromiumBase +class BrowserCookiesSetter(object): + _owner: Chromium = ... - def __init__(self, page: ChromiumBase): ... + def __init__(self, page: Chromium): ... def __call__(self, cookies: Union[CookieJar, Cookie, list, tuple, str, dict]) -> None: ... - def remove(self, name: str, url: str = None, domain: str = None, path: str = None) -> None: ... - def clear(self) -> None: ... -class BrowserCookiesSetter(CookiesSetter): - _owner: Chromium = ... +class CookiesSetter(BrowserCookiesSetter): + _owner: ChromiumBase = ... - def __init__(self, page: Chromium): ... + def __init__(self, page: ChromiumBase): ... + + def remove(self, name: str, url: str = None, domain: str = None, path: str = None) -> None: ... + + def clear(self) -> None: ... class SessionCookiesSetter(object): diff --git a/DrissionPage/_units/waiter.py b/DrissionPage/_units/waiter.py index f471114..3486cb6 100644 --- a/DrissionPage/_units/waiter.py +++ b/DrissionPage/_units/waiter.py @@ -30,6 +30,27 @@ class BrowserWaiter(OriginWaiter): def __init__(self, owner): self._owner = owner + def new_tab(self, timeout=None, curr_tab=None, raise_err=None): + """等待新标签页出现 + :param timeout: 超时时间(秒),为None则使用页面对象timeout属性 + :param curr_tab: 指定当前最新的tab id,用于判断新tab出现,为None自动获取 + :param raise_err: 等待失败时是否报错,为None时根据Settings设置 + :return: 等到新标签页返回其id,否则返回False + """ + curr_tid = curr_tab if curr_tab else self._owner.tab_ids[0] + timeout = timeout if timeout is not None else self._owner.timeout + end_time = perf_counter() + timeout + while perf_counter() < end_time: + latest_tid = self._owner.tab_ids[0] + if curr_tid != latest_tid: + return latest_tid + sleep(.01) + + if raise_err is True or Settings.raise_when_wait_failed is True: + raise WaitTimeoutError(f'等待新标签页失败(等待{timeout}秒)。') + else: + return False + def download_begin(self, timeout=None, cancel_it=False): """等待浏览器下载开始,可将其拦截 :param timeout: 超时时间(秒),None使用页面对象超时时间 @@ -54,27 +75,6 @@ class BrowserWaiter(OriginWaiter): self._owner._dl_mgr.set_flag('browser', None) return r - def new_tab(self, timeout=None, curr_tab=None, raise_err=None): - """等待新标签页出现 - :param timeout: 超时时间(秒),为None则使用页面对象timeout属性 - :param curr_tab: 指定当前最新的tab id,用于判断新tab出现,为None自动获取 - :param raise_err: 等待失败时是否报错,为None时根据Settings设置 - :return: 等到新标签页返回其id,否则返回False - """ - curr_tid = curr_tab if curr_tab else self._owner.tab_ids[0] - timeout = timeout if timeout is not None else self._owner.timeout - end_time = perf_counter() + timeout - while perf_counter() < end_time: - latest_tid = self._owner.tab_ids[0] - if curr_tid != latest_tid: - return latest_tid - sleep(.01) - - if raise_err is True or Settings.raise_when_wait_failed is True: - raise WaitTimeoutError(f'等待新标签页失败(等待{timeout}秒)。') - else: - return False - def all_downloads_done(self, timeout=None, cancel_if_timeout=True): """等待所有浏览器下载任务结束 :param timeout: 超时时间(秒),为None时无限等待 @@ -332,6 +332,7 @@ class BaseWaiter(OriginWaiter): class TabWaiter(BaseWaiter): + """标签页对象等待对象""" def downloads_done(self, timeout=None, cancel_if_timeout=True): """等待所有浏览器下载任务结束 @@ -370,6 +371,8 @@ class TabWaiter(BaseWaiter): class PageWaiter(TabWaiter): + """ChromiumPage和MixPage的等待对象""" + def __init__(self, page): super().__init__(page) @@ -379,18 +382,7 @@ class PageWaiter(TabWaiter): :param raise_err: 等待失败时是否报错,为None时根据Settings设置 :return: 等到新标签页返回其id,否则返回False """ - timeout = timeout if timeout is not None else self._driver.timeout - end_time = perf_counter() + timeout - while perf_counter() < end_time: - latest_tid = self._driver.tab_ids[0] - if self._driver.tab_id != latest_tid: - return latest_tid - sleep(.01) - - if raise_err is True or Settings.raise_when_wait_failed is True: - raise WaitTimeoutError(f'等待新标签页失败(等待{timeout}秒)。') - else: - return False + return self._driver.browser.wait.new_tab(timeout=timeout, raise_err=raise_err) def all_downloads_done(self, timeout=None, cancel_if_timeout=True): """等待所有浏览器下载任务结束 @@ -398,27 +390,7 @@ class PageWaiter(TabWaiter): :param cancel_if_timeout: 超时时是否取消剩余任务 :return: 是否等待成功 """ - if not self._driver.browser._dl_mgr._running: - raise RuntimeError('此功能需显式设置下载路径(使用set.download_path()方法、配置对象或ini文件均可)。') - if not timeout: - while self._driver.browser._dl_mgr._missions: - sleep(.5) - return True - - else: - end_time = perf_counter() + timeout - while perf_counter() < end_time: - if not self._driver.browser._dl_mgr._missions: - return True - sleep(.5) - - if self._driver.browser._dl_mgr._missions: - if cancel_if_timeout: - for m in list(self._driver.browser._dl_mgr._missions.values()): - m.cancel() - return False - else: - return True + return self._driver.browser.wait.all_downloads_done(timeout=timeout, cancel_if_timeout=cancel_if_timeout) class ElementWaiter(OriginWaiter): @@ -427,7 +399,7 @@ class ElementWaiter(OriginWaiter): def __init__(self, owner, ele): """等待元素在dom中某种状态,如删除、显示、隐藏 :param owner: 元素所在页面 - :param ele: 要等待的元素 + :param ele: 要执行等待的元素 """ self._owner = owner self._ele = ele