4.1.0.0b3修复清空和删除cookies问题

This commit is contained in:
g1879 2024-07-09 21:43:12 +08:00
parent 2ea2be782f
commit 58285fc0ad
4 changed files with 61 additions and 81 deletions

View File

@ -14,4 +14,4 @@ from ._pages.chromium_page import ChromiumPage
from ._pages.mix_page import MixPage from ._pages.mix_page import MixPage
from ._pages.mix_page import MixPage as WebPage from ._pages.mix_page import MixPage as WebPage
__version__ = '4.1.0.0b2' __version__ = '4.1.0.0b3'

View File

@ -8,13 +8,27 @@
from .._functions.cookies import set_tab_cookies, set_session_cookies, set_browser_cookies from .._functions.cookies import set_tab_cookies, set_session_cookies, set_browser_cookies
class CookiesSetter(object): class BrowserCookiesSetter(object):
def __init__(self, owner): def __init__(self, owner):
""" """
:param owner: ChromiumBase对象 :param owner: Chromium对象
""" """
self._owner = owner 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): def __call__(self, cookies):
"""设置一个或多个cookie """设置一个或多个cookie
:param cookies: cookies信息 :param cookies: cookies信息
@ -22,6 +36,10 @@ class CookiesSetter(object):
""" """
set_tab_cookies(self._owner, cookies) 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): def remove(self, name, url=None, domain=None, path=None):
"""删除一个cookie """删除一个cookie
:param name: cookie的name字段 :param name: cookie的name字段
@ -37,23 +55,11 @@ class CookiesSetter(object):
d['domain'] = domain d['domain'] = domain
if not url and not domain: if not url and not domain:
d['url'] = self._owner.url d['url'] = self._owner.url
if not d['url'].startswith('http'):
raise ValueError('需设置domain或url值。如设置url值需以http开头。')
if path is not None: if path is not None:
d['path'] = path d['path'] = path
self._owner._run_cdp('Storage.deleteCookies', **d) self._owner._run_cdp('Network.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)
class SessionCookiesSetter(object): class SessionCookiesSetter(object):

View File

@ -15,22 +15,24 @@ from .._pages.session_page import SessionPage
from .._pages.mix_page import MixPage from .._pages.mix_page import MixPage
class CookiesSetter(object): class BrowserCookiesSetter(object):
_owner: ChromiumBase _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 __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: ... def clear(self) -> None: ...
class BrowserCookiesSetter(CookiesSetter): class CookiesSetter(BrowserCookiesSetter):
_owner: Chromium = ... _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): class SessionCookiesSetter(object):

View File

@ -30,6 +30,27 @@ class BrowserWaiter(OriginWaiter):
def __init__(self, owner): def __init__(self, owner):
self._owner = 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): def download_begin(self, timeout=None, cancel_it=False):
"""等待浏览器下载开始,可将其拦截 """等待浏览器下载开始,可将其拦截
:param timeout: 超时时间None使用页面对象超时时间 :param timeout: 超时时间None使用页面对象超时时间
@ -54,27 +75,6 @@ class BrowserWaiter(OriginWaiter):
self._owner._dl_mgr.set_flag('browser', None) self._owner._dl_mgr.set_flag('browser', None)
return r 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): def all_downloads_done(self, timeout=None, cancel_if_timeout=True):
"""等待所有浏览器下载任务结束 """等待所有浏览器下载任务结束
:param timeout: 超时时间为None时无限等待 :param timeout: 超时时间为None时无限等待
@ -332,6 +332,7 @@ class BaseWaiter(OriginWaiter):
class TabWaiter(BaseWaiter): class TabWaiter(BaseWaiter):
"""标签页对象等待对象"""
def downloads_done(self, timeout=None, cancel_if_timeout=True): def downloads_done(self, timeout=None, cancel_if_timeout=True):
"""等待所有浏览器下载任务结束 """等待所有浏览器下载任务结束
@ -370,6 +371,8 @@ class TabWaiter(BaseWaiter):
class PageWaiter(TabWaiter): class PageWaiter(TabWaiter):
"""ChromiumPage和MixPage的等待对象"""
def __init__(self, page): def __init__(self, page):
super().__init__(page) super().__init__(page)
@ -379,18 +382,7 @@ class PageWaiter(TabWaiter):
:param raise_err: 等待失败时是否报错为None时根据Settings设置 :param raise_err: 等待失败时是否报错为None时根据Settings设置
:return: 等到新标签页返回其id否则返回False :return: 等到新标签页返回其id否则返回False
""" """
timeout = timeout if timeout is not None else self._driver.timeout return self._driver.browser.wait.new_tab(timeout=timeout, raise_err=raise_err)
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
def all_downloads_done(self, timeout=None, cancel_if_timeout=True): def all_downloads_done(self, timeout=None, cancel_if_timeout=True):
"""等待所有浏览器下载任务结束 """等待所有浏览器下载任务结束
@ -398,27 +390,7 @@ class PageWaiter(TabWaiter):
:param cancel_if_timeout: 超时时是否取消剩余任务 :param cancel_if_timeout: 超时时是否取消剩余任务
:return: 是否等待成功 :return: 是否等待成功
""" """
if not self._driver.browser._dl_mgr._running: return self._driver.browser.wait.all_downloads_done(timeout=timeout, cancel_if_timeout=cancel_if_timeout)
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
class ElementWaiter(OriginWaiter): class ElementWaiter(OriginWaiter):
@ -427,7 +399,7 @@ class ElementWaiter(OriginWaiter):
def __init__(self, owner, ele): def __init__(self, owner, ele):
"""等待元素在dom中某种状态如删除、显示、隐藏 """等待元素在dom中某种状态如删除、显示、隐藏
:param owner: 元素所在页面 :param owner: 元素所在页面
:param ele: 等待的元素 :param ele: 执行等待的元素
""" """
self._owner = owner self._owner = owner
self._ele = ele self._ele = ele